我一直在使用Java APT进行一些注释处理,并且我能够在TDD练习之后从带注释的类中生成.java文件,只要我生成.java文件,就可以进行测试。我使用了google-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源代码内容吗?
答案 0 :(得分: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));