Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-project test dependencies with gradle

I have a multi-project configuration and I want to use gradle.

My projects are like this:

  • Project A

    • -> src/main/java
    • -> src/test/java
  • Project B

    • -> src/main/java (depends on src/main/java on Project A)
    • -> src/test/java (depends on src/test/java on Project A)

My Project B build.gradle file is like this:

apply plugin: 'java' dependencies {   compile project(':ProjectA') } 

The task compileJava work great but the compileTestJava does not compile the test file from Project A.

like image 405
mathd Avatar asked Apr 13 '11 03:04

mathd


People also ask

Does Gradle run tests in parallel?

One of the best features in Gradle for JVM-related projects is its ability to run tests in parallel. As discussed in the Gradle documentation, this implemented by setting the maxParallelForks property inside a test block in the build.

How do I sync Gradle with dependencies?

Simply open the gradle tab (can be located on the right) and right-click on the parent in the list (should be called "Android"), then select "Refresh dependencies". This should resolve your issue.


2 Answers

This is now supported as a first class feature in Gradle. Modules with java or java-library plugins can also include a java-test-fixtures plugin which exposes helper classes and resources to be consumed with testFixtures helper. Benefit of this approach against artifacts and classifiers are:

  • proper dependency management (implementation/api)
  • nice separation from test code (separate source set)
  • no need to filter out test classes to expose only utilities
  • maintained by Gradle

Example

:modul:one

modul/one/build.gradle

plugins {   id "java-library" // or "java"   id "java-test-fixtures" } 

modul/one/src/testFixtures/java/com/example/Helper.java

package com.example; public class Helper {} 

:modul:other

modul/other/build.gradle

plugins {   id "java" // or "java-library" } dependencies {   testImplementation(testFixtures(project(":modul:one"))) } 

modul/other/src/test/java/com/example/other/SomeTest.java

package com.example.other; import com.example.Helper; public class SomeTest {   @Test void f() {     new Helper(); // used from :modul:one's testFixtures   } } 

Further reading

For more info, see the documentation:
https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures

It was added in 5.6:
https://docs.gradle.org/5.6/release-notes.html#test-fixtures-for-java-projects

like image 40
TWiStErRob Avatar answered Oct 16 '22 20:10

TWiStErRob


Deprecated - For Gradle 5.6 and above use this answer.

In Project B, you just need to add a testCompile dependency:

dependencies {   ...   testCompile project(':A').sourceSets.test.output } 

Tested with Gradle 1.7.

like image 56
Fesler Avatar answered Oct 16 '22 18:10

Fesler