0
在GWT中,發電機的代碼位於rebind包中。該軟件包不包含在模塊xml文件中,因爲它可能包含非客戶端代碼。如何測試GWT中的Generator?
這是我做的。我的模塊文件是/test/gwttest.gwt.xml。它包含以下內容:
<generate-with class="test.rebind.FunctionGenerator">
<when-type-assignable class="test.client.Function" />
</generate-with>
<source path='client'/>
這裏是/測試/客戶機/功能:
public interface Function {
public Object execute();
}
我Generator是/測試/重新綁定/函數發生器:
public class FunctionGenerator extends Generator {
private static final String IMPL_TYPE_NAME = Function.class.getSimpleName()
+ "Impl";
private static final String IMPL_PACKAGE_NAME = Function.class.getPackage()
.getName();
@Override
public String generate(TreeLogger logger, GeneratorContext context,
String requestedClass) throws UnableToCompleteException {
TypeOracle typeOracle = context.getTypeOracle();
JClassType functionType = typeOracle.findType(requestedClass);
assert Function.class.equals(functionType.getClass());
ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(
IMPL_PACKAGE_NAME, IMPL_TYPE_NAME);
composerFactory.addImport(Function.class.getCanonicalName());
composerFactory.addImplementedInterface(Function.class.getName());
PrintWriter printWriter = context.tryCreate(logger, IMPL_PACKAGE_NAME,
IMPL_TYPE_NAME);
SourceWriter sourceWriter = composerFactory.createSourceWriter(context,
printWriter);
sourceWriter.print("public Object execute() {");
sourceWriter.print(" return 1;");
sourceWriter.print("}");
sourceWriter.commit(logger);
return IMPL_PACKAGE_NAME + "." + IMPL_TYPE_NAME;
}
}
我GeneratorTest是/測試/ rebind/FunctionGeneratorTest:
public class FunctionGeneratorTest extends GWTTestCase {
@Override
public String getModuleName() {
return "com.acme.gwt.Generator";
}
public void testGenerator() throws Exception {
Function function = GWT.create(Function.class);
assertNotNull(function);
assertEquals(1, function.execute());
}
}
測試總是失敗,因爲這個錯誤:
com.google.gwt.junit.JUnitFatalLaunchException: The test class 'test.rebind.FunctionGeneratorTest' was not found in module 'test.Gwttest'; no compilation unit for that type was seen
at com.google.gwt.junit.JUnitShell.checkTestClassInCurrentModule(JUnitShell.java:766)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1349)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1311)
at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:705)
at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:421)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:247)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我不能包括重新綁定的文件夾在我的模塊文件源,因爲它沒有客戶端的代碼。
我該怎麼辦?
當我將測試從/ test/rebind移動到/ test/client時,它適用於Eclipse Run as GWT Unit Test。
當我嘗試Eclipse Run as GWT Unit Test(生產模式)時,測試失敗。
這裏是跟蹤:
com.google.gwt.core.ext.UnableToCompleteException: (see previous log entries)
at com.google.gwt.junit.JUnitShell.compileForWebMode(JUnitShell.java:1125)
at com.google.gwt.junit.JUnitShell.maybeCompileForWebMode(JUnitShell.java:1174)
at com.google.gwt.junit.CompileStrategy.maybeCompileModuleImpl2(CompileStrategy.java:180)
at com.google.gwt.junit.CompileStrategy.maybeCompileModuleImpl(CompileStrategy.java:112)
at com.google.gwt.junit.SimpleCompileStrategy.maybeCompileModule(SimpleCompileStrategy.java:36)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1342)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1311)
at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:705)
at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:421)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:247)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
爲什麼測試在生產模式失敗,並將在開發模式?
您是否知道一小會兒,標誌着代碼現在GWT已經支持註解「無法對客戶端「?那會處理你需要的嗎? – chrylis
@chrylis這是什麼意思?你有這樣的例子嗎? – confile
可能是測試文件夾沒有添加在class-path中。 – Braj