GWT单元测试用例 [英] GWT unit test case
问题描述
我得到的错误是,错误:GWT.create() )仅在客户端代码中可用!例如,它不能从服务器代码中调用。如果您正在运行单元测试,请检查您的测试用例是否扩展了GWTTestCase,并且GWT.create()不是从初始化程序或构造函数中调用的。 b
$ b
当我调试时,我发现这个错误是源自行
$ b $ p
NumberFormat formatter = NumberFormat.getCurrencyFormat(); 在我的代码中。
这是我的代码:
TestCase:
public class CurrencyFormatterTest extends GWTTestCase {
private CurrencyFormatter currencyFormatter = new CurrencyFormatter();
public void testCurrencyFormatForActualCurrencyString(){
String formattedString = currencyFormatter.format(123456.78999);
assertEquals(US $ 123,456.79,formattedString);
}
@Override
public String getModuleName(){
return null;
$ / code>
要测试的代码
public class CurrencyFormatter implements Formatter {
$ b $ @Override
public String format(Object value){
if (value == null || value.equals()){
return;
}
Double val = Double.parseDouble(value.toString());
NumberFormat formatter = NumberFormat.getCurrencyFormat();
String formattedValue = formatter.format(val);
返回formattedValue;
}
}
如果 getModuleName
返回 null
,那么您的测试就像任何其他JUnit测试一样运行,即 not 在 GWT上下文中。
使 getModuleName
模块(您传递给GWT编译器的那个模块,或者另一个GWT模块中的<继承>
)切换为以GWT模式运行 em>。
I was planning to write a unit test case for one of my helper methods in GWT.
Am getting error saying that ERROR: GWT.create() is only usable in client code! It cannot be called, for example, from server code. If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor.
When I debug, I see that error is originating from the line
NumberFormat formatter = NumberFormat.getCurrencyFormat();
in my code. Kindly help.
This is my code :
TestCase:
public class CurrencyFormatterTest extends GWTTestCase {
private CurrencyFormatter currencyFormatter = new CurrencyFormatter();
public void testCurrencyFormatForActualCurrencyString() {
String formattedString = currencyFormatter.format( " 123456.78999" );
assertEquals( "US$123,456.79", formattedString );
}
@Override
public String getModuleName() {
return null;
}
}
Code to be Tested
public class CurrencyFormatter implements Formatter {
@Override
public String format( Object value ) {
if ( value == null || value.equals( "" ) ) {
return "";
}
Double val = Double.parseDouble( value.toString() );
NumberFormat formatter = NumberFormat.getCurrencyFormat();
String formattedValue = formatter.format( val );
return formattedValue;
}
}
If getModuleName
returns null
then your test runs just like any other JUnit test, i.e. not in a GWT context.
Make getModuleName
return the name of your module (the one you pass to the GWT Compiler, or the one you'll <inherit>
in another GWT module) to switch to running as GWT.
这篇关于GWT单元测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!