Search code examples
unit-testingjunitgradlemulti-project

UnitTest with gradle, when test Infrastructure is in some other project in src/test/java folder


There is two java projects A and B, the build-engine is gradle.

That is a multi-project and A depends on B

A: build.gradle

...
dependencies {
    compile project (':B')
}
...

B has core code in src/main/java and test code in src/test/java. Also in src/test/java is some testing infrastructure classes like loading of test-data.

gradle :B:test work fine

Tests in A-Project uses the testing infrastructure classes from B-Project, and that is alright for eclipse, but not for gradle. Because the builded jar-file dependency on B-Project includes only classes from src/main/java.

And we get the error:

com.aaa.SomeClassFromAAAA.java:42: error: cannot find symbol
import com.bbb.InfrastructureTestClassFromBBB;

What is the solution?


Solution

  • This is discussed on Gradle forum here. The idea is to build JAR with your test classes and make it visible to your A project. There are also notes why this is difficult for the IDEs.

    The alternative way is to move test infrastructure classes into main sources of another project. This assumes they do not depend on A's main sources. You can see something similar in Gradle's codebase (int*-test* folders in https://github.com/gradle/gradle/tree/master/subprojects).