Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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
如何测试注解处理器生成的none.java源代码?_Java_Unit Testing_Code Generation_Annotation Processing_Annotation Processor - Fatal编程技术网

如何测试注解处理器生成的none.java源代码?

如何测试注解处理器生成的none.java源代码?,java,unit-testing,code-generation,annotation-processing,annotation-processor,Java,Unit Testing,Code Generation,Annotation Processing,Annotation Processor,我一直在使用Java APT进行一些注释处理,并且我能够根据TDD实践从注释类中生成.Java文件,只要我生成.Java文件,一切都很好,用于测试我使用的,下面是我如何对生成的源代码进行测试: @Test public void testGeneratedSource() throws Exception { Truth.assert_().about(javaSource()).that(JavaFileObjects.forResource("source class .java g

我一直在使用Java APT进行一些注释处理,并且我能够根据TDD实践从注释类中生成.Java文件,只要我生成.Java文件,一切都很好,用于测试我使用的,下面是我如何对生成的源代码进行测试:

@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可以生成任何类型的源代码,但我认为这里的问题在于我为测试所做的调用或参数,我一直在试图找出如何使用测试工具来测试无java生成的源代码,但运气不佳


有人知道如何用java源代码测试.html或其他内容吗?

也许使用另一种JavaFileObjects方法会有所帮助:.forResource(URL)

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


我找到了这个问题的答案,但我无法发布它,因为我被禁止回答,我将在我可以发布答案后发布。这里的getResourceURL是什么?请参阅:受保护的静态URL getResourceURL(String projectSrcRoot,String resourcePath)抛出错误的异常{final Path tmpPath=Path.get(projectSrcRoot+resourcePath);final URL resourceURL=tmpPath.toAbsolutePath().toUri().tour();return resourceURL;}另外还有一个方法。with字符串内容
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));