How to run JUnit tests with Gradle?

JavaGradleJunit4build.gradle

Java Problem Overview


Currently I have the following build.gradle file:

apply plugin: 'java'

sourceSets {
    main {
        java {
            srcDir 'src/model'
        }
    }
}

dependencies {
    compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
    runtime fileTree(dir: 'libs', include: '*.jar')
}    


This build.gradle file is for my repository here. All of my main files are in src/model/ and their respective tests are in test/model.

How do I add a JUnit 4 dependency correctly and then run those tests in the folders of tests/model?

Java Solutions


Solution 1 - Java

> How do I add a junit 4 dependency correctly?

Assuming you're resolving against a standard Maven (or equivalent) repo:

dependencies {
    ...
    testCompile "junit:junit:4.11"  // Or whatever version
}

> Run those tests in the folders of tests/model?

You define your test source set the same way:

sourceSets {
    ...

    test {
        java {
            srcDirs = ["test/model"]  // Note @Peter's comment below
        }
    }
}
             

Then invoke the tests as:

./gradlew test

EDIT: If you are using JUnit 5 instead, there are more steps to complete, you should follow this tutorial.

Solution 2 - Java

If you set up your project with the default gradle package structure, i.e.:

src/main/java
src/main/resources
src/test/java
src/test/resources

then you won't need to modify sourceSets to run your tests. Gradle will figure out that your test classes and resources are in src/test. You can then run as Oliver says above. One thing to note: Be careful when setting property files and running your test classes with both gradle and you IDE. I use Eclipse, and when running JUnit from it, Eclipse chooses one classpath (the bin directory) whereas gradle chooses another (the build directory). This can lead to confusion if you edit a resource file, and don't see your change reflected at test runtime.

Solution 3 - Java

If you created your project with Spring Initializr, everything should be configured correctly and all you need to do is run...

./gradlew clean test --info
  • Use --info if you want to see test output.
  • Use clean if you want to re-run tests that have already passed since the last change.

Dependencies required in build.gradle for testing in Spring Boot...

dependencies {
	compile('org.springframework.boot:spring-boot-starter')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

For some reason the test runner doesn't tell you this, but it produces an HTML report in build/reports/tests/test/index.html.

Solution 4 - Java

testCompile is deprecated. Gradle 7 compatible:

dependencies {
...
   testImplementation 'junit:junit:4.13'
}

and if you use the default folder structure (src/test/java/...) the test section is simply:

test {
    useJUnit()
}

Finally:

gradlew clean test

Alos see: https://docs.gradle.org/current/userguide/java_testing.html

Solution 5 - Java

This is for Kotlin DSL (build.gradle.kts) and using JUnit 5 (JUnit platform):

tasks.test {
    // Discover and execute JUnit4-based tests
    useJUnit()

    // Discover and execute TestNG-based tests
    useTestNG()

    // Discover and execute JUnit Platform-based (JUnit 5, JUnit Jupiter) tests
    // Note that JUnit 5 has the ability to execute JUnit 4 tests as well
    useJUnitPlatform()
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
    // ...
}

Solution 6 - Java

If you want to add a sourceSet for testing in addition to all the existing ones, within a module regardless of the active flavor:

sourceSets {
    test {
        java.srcDirs += [                'src/customDir/test/kotlin'        ]
        print(java.srcDirs)   // Clean
    }
}

Pay attention to the operator += and if you want to run integration tests change test to androidTest.

GL

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWang-Zhao-Liu QView Question on Stackoverflow
Solution 1 - JavaOliver CharlesworthView Answer on Stackoverflow
Solution 2 - JavaDivDiffView Answer on Stackoverflow
Solution 3 - JavaBrent BradburnView Answer on Stackoverflow
Solution 4 - Javarob2universeView Answer on Stackoverflow
Solution 5 - JavaMahozadView Answer on Stackoverflow
Solution 6 - JavaBraian CoronelView Answer on Stackoverflow