Truth.assertAbout 和 JavaSourceSubjectFactory.javaSource()
Truth.assertAbout and JavaSourceSubjectFactory.javaSource()
我正在编写注释处理器并希望使用 google-compile-testing
和 truth
为其编写一些单元测试:
所以我想写一个非常简单的单元测试。
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");
}
所以基本上我从 google 的 dagger2 存储库中复制并粘贴了一个简单的测试,并用我的注解处理器替换了相关数据。 我正在使用 maven,并且我正在使用与 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>
但是我无法编译代码。我猜是泛型参数有问题,但不知道是什么问题。
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>
任何提示我做错了什么?我找不到与 google dagger2 测试(顺便在我的机器上编译)有任何区别
真理的神器com.google.testing.compile:compile-testing:0.5
depends on org.truth0:truth:0.15
which is the old location。尝试使用 compile-testing
的 0.6
版本。