I'm writing an annotation processor and want to write some unit tests for it by using google-compile-testing
and truth
:
So I want to write a very simple unit test.
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
@Test
public void componentOnConcreteClass() {
JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.NotAClass",
"package test;",
"",
"import my.annotation.MyAnnotation;",
"",
"@MyAnnotation",
"interface NotAComponent {}");
assertAbout(javaSource()).that(componentFile)
.processedWith(new MyProcessor())
.failsToCompile()
.withErrorContaining("interface");
}
So basically I have copy an pasted a simple test from google's dagger2 repo and replaced the relevant data with my annotation processor. I'm using maven, and I'm using the same dependencies as dagger2:
<dependency>
<groupId>com.google.testing.compile</groupId>
<artifactId>compile-testing</artifactId>
<version>0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
But I can't compile the code. I guess there is an generics param problem, but can't figure out what the problem is.
Compilation failure:
[ERROR] ProcessorTest.java:[46,5] method assertAbout in class com.google.common.truth.Truth cannot be applied to given types;
[ERROR] required: com.google.common.truth.SubjectFactory<S,T>
[ERROR] found: com.google.testing.compile.JavaSourceSubjectFactory
[ERROR] reason: no instance(s) of type variable(s) S,T exist so that argument type com.google.testing.compile.JavaSourceSubjectFactory conforms to formal parameter type com.google.common.truth.SubjectFactory<S,T>
Any hint what I'm doing wrong? I can't find any difference to google dagger2 tests (which by the way compiles on my machine)
The artifact com.google.testing.compile:compile-testing:0.5
depends on org.truth0:truth:0.15
which is the old location of Truth. Try using version 0.6
of compile-testing
instead.