我有一个依赖普通Java库B的Android库A.
在A的检测测试中,我希望能够执行B的某些测试来断言某些Java类型的行为(在Android中有一个特殊的实现)。
我尝试将此解决方案(https://stackoverflow.com/a/5153162/6307827)应用于A和B:
B /的build.gradle:
...
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
from sourceSets.test.output
}
configurations {
tests
}
artifacts {
tests testJar
}
A /的build.gradle:
dependencies {
...
testCompile 'junit:junit:4.12'
androidTestCompile project(path: ':B', configuration: 'tests')
// testCompile project(path: ':B', configuration: 'tests')
androidTestCompile 'com.android.support:support-annotations:25.3.1'
androidTestCompile 'com.android.support.test:runner:0.5'
}
检测测试A / src / androidTest / java /.../ JavaTests.java:
package ...;
import android.view.View; // import at least one Android class
import org.junit.Test;
import ....reflect.MyTest; // in B
import static org.junit.Assert.*;
public class JavaTests {
@Test
public void main() throws Exception {
assertEquals("android.view.View", View.class.getName());
MyTest.main(null);
}
}
运行JavaTests时,我收到以下消息:
Information:Gradle tasks [:A:assembleDebug, :A:assembleDebugAndroidTest]
...
.../A/src/androidTest/java/.../JavaTests.java
Error:(...) error: cannot find symbol class MyTest
Error:org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
如何让A测试访问两个B源集的任何类?