📅  最后修改于: 2021-01-02 12:52:30             🧑  作者: Mango
JUnit提供了经过时间检验的框架来测试GWT应用程序。它包含许多工具,可以根据用户需求直接创建测试用例。
现在,基于Web项目STOCK EXCHANGE (在上一章中创建),我们执行JUnit测试。
StockExchangeTest.java
package com.google.gwt.sample.stockexchange.client;
import com.google.gwt.junit.client.GWTTestCase;
/**
* GWT JUnit tests must extend GWTTestCase.
*/
public class StockExchangeTest extends GWTTestCase {
/**
* Must refer to a valid module that sources this class.
*/
public String getModuleName() {
return "com.google.gwt.sample.stockexchange.StockExchange";
}
/**
* Add as many tests as you like.
*/
public void testSimple() {
assertTrue(true);
}
}
您可以通过四种方式运行JUnit测试:
我们正在使用Eclipse和Google插件:
Google Eclipse插件可轻松在Eclipse中运行测试。
/**
* Verify that the instance fields in the StockPrice class are set correctly.
*/
public void testStockPriceCtor() {
String symbol = "XYZ";
double price = 70.0;
double change = 2.0;
double changePercent = 100.0 * change / price;
StockPrice sp = new StockPrice(symbol, price, change);
assertNotNull(sp);
assertEquals(symbol, sp.getSymbol());
assertEquals(price, sp.getPrice(), 0.001);
assertEquals(change, sp.getChange(), 0.001);
assertEquals(changePercent, sp.getChangePercent(), 0.001);
}
在开发模式下重新运行StockExchangeTest。
两项测试均应通过:
[junit] Running com.google.gwt.sample.stockexchange.client.StockExchangeTest
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 12.451 sec