Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在GWT中测试发电机?_Java_Javascript_Unit Testing_Gwt_Junit - Fatal编程技术网

Java 如何在GWT中测试发电机?

Java 如何在GWT中测试发电机?,java,javascript,unit-testing,gwt,junit,Java,Javascript,Unit Testing,Gwt,Junit,在GWT中,发电机代码位于重新绑定包中。此包不包含在模块xml文件中,因为它可能包含非客户端代码 这就是我所做的。我的模块文件是/test/gwtest.gwt.xml。它包含以下内容: <generate-with class="test.rebind.FunctionGenerator"> <when-type-assignable class="test.client.Function" /> </generate-with> <sour

在GWT中,发电机代码位于重新绑定包中。此包不包含在模块xml文件中,因为它可能包含非客户端代码

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

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

<source path='client'/>
我的生成器是/测试/重新绑定/函数生成器:

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)
我无法将重新绑定文件夹作为源文件包含在模块文件中,因为它不是客户端代码

我必须做什么?

当我将测试从/test/rebind移动到/test/client时,它与Eclipse Run as>GWT单元测试一起工作

当我尝试Eclipse Run as>GWT单元测试(生产模式)时,测试失败

以下是跟踪:

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)
为什么测试在生产模式下失败,而在开发模式下通过?请参阅以下链接:

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

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

您是否知道GWT支持标记代码“不适用于客户端”的注释有一段时间了?这能满足你的需要吗?@chrylis这是什么意思?你有这样的例子吗?可能是测试文件夹没有添加到类路径中。@Braj我写过,运行GWT单元测试可以工作,所以测试文件夹在类路径中。不起作用的是生产测试。
PrintWriter printWriter = context.tryCreate(logger, IMPL_PACKAGE_NAME, IMPL_TYPE_NAME);
if (printWriter != null) {
....
} 
return IMPL_PACKAGE_NAME + "." + IMPL_TYPE_NAME;