2

我一直在使用 Java APT 进行一些注释处理,并且我能够按照 TDD 实践从带注释的类中生成 .java 文件,只要我生成 .java 文件,一切都做得很好,为了测试我使用了谷歌-compile-testing,这是我对生成的源代码进行测试的方式:

@Test
public void testGeneratedSource() throws Exception {
    Truth.assert_().about(javaSource()).that(JavaFileObjects.forResource("source class .java goes here"))
            .processedWith(new UiFormProcessor()).compilesWithoutError()
            .and()
            .generatesSources(JavaFileObjects.forSourceString("","expected generated class content goes here"));

}

有了这个,我能够检查并验证生成的 .java 文件。

但后来我想生成一些 .html 文件,并且我希望能够测试我生成的 html 内容。

但使用上述方法失败,因为它需要 .java 文件,并引发以下断言错误

java.lang.AssertionError: Did not find a generated file corresponding to .java

at com.google.common.truth.FailureStrategy.fail(FailureStrategy.java:27)
at com.google.common.truth.FailureStrategy.fail(FailureStrategy.java:23)
at com.google.testing.compile.JavaSourcesSubject$SuccessfulCompilationBuilder.generatesFiles(JavaSourcesSubject.java:491)
at com.progressoft.annotation.processor.UiFormsTest.emptyAnnotatedClass_shouldGenerateEmptyHtmlFormTemplate(UiFormsTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

该错误表明我应该生成 .java 文件,但我知道使用 APT 您可以生成任何类型的源代码,但我认为这里的问题在于我为测试所做的调用或参数,我一直在试图弄清楚如何使用google-compile-testing测试没有 java 生成的源代码,但没有运气。

有人知道如何使用google-compile-testing测试 .html 或其他 java 源的任何其他内容吗?

4

1 回答 1

1

也许它可以帮助使用另一个 JavaFileObjects 方法:.forResource(URL)

我认为这可行:(路径是示例)

URL urlPersonJava = getResourceURL("src/test/java/", 
                              "com/github/funthomas424242/domain/Person.java");
URL urlPersonBuilderHtml = getResourceURL("src/test/expectations/", 
                              "PersonBuilder.html");

assertAbout(javaSource())
        .that(JavaFileObjects.forResource(urlPersonJava))
        .processedWith(processor1)
        .compilesWithoutError()
        .and()               
        .generatesSources(JavaFileObjects.forResource(urlPersonBuilderHtml));
于 2018-05-06T17:51:34.490 回答