java - 如何在 GWT 中测试生成器?

标签 java javascript unit-testing gwt junit

在 GWT 中,生成器的代码位于 rebind 包中。该包不包含在模块 xml 文件中,因为它可能包含非客户端代码。

这就是我所做的。我的模块文件是/test/gwttest.gwt.xml。它包含以下内容:

<generate-with class="test.rebind.FunctionGenerator">
    <when-type-assignable class="test.client.Function" />
</generate-with>

<source path='client'/>

这里是/test/client/Function:

public interface Function {
    public Object execute();
}

我的生成器是/test/rebind/FunctionGenerator:

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 是/test/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)

我无法将重新绑定(bind)文件夹作为源包含在我的模块文件中,因为它不是客户端代码。

我必须做什么?

当我将测试从/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)

为什么测试在生产模式下失败而在开发模式下通过?

最佳答案

查看此链接:https://groups.google.com/forum/#!topic/google-web-toolkit/OyH839KEH-w

这将有助于使生产代码正常工作:

PrintWriter printWriter = context.tryCreate(logger, IMPL_PACKAGE_NAME, IMPL_TYPE_NAME);
if (printWriter != null) {
....
} 
return IMPL_PACKAGE_NAME + "." + IMPL_TYPE_NAME;

关于java - 如何在 GWT 中测试生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23567148/

相关文章:

java - fragment 未显示在 Android 导航组件中

java - Spring 管理事务、EclipseLink JPA、自定义隔离级别

javascript - 使用 sinon 提交表单时如何测试函数是否被调用?

Angular Testing 失败,无法在 'send' 上执行 'XMLHttpRequest'

ios - 如何在 Alamofire 响应上 XCTAssertEqual?

java - java中的对象引用集

java - 有没有更好的方法来使用 Twitter4J 捕捉推文?

javascript jQuery 变量作用域

javascript - eventListener 'resize' 不会更改 Vue.js 中 data() 变量的值

javascript - 隐藏基于当前日期的下拉选项