Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep Java code and Junit tests together building with Gradle

Tags:

gradle

I have a project in which the main source and the test cases for that source are kept in the same package/directory. Each test class is the name of the class which it is testing with "Test" appended on the end. So if I have a Foo.java there will be a FooTest.java right next to it.

My question is, how do I build this project with Gradle? I'd still like to keep the class files separate, i.e. a folder for main classes and a folder for test classes.

like image 728
MrQBerrt Avatar asked Mar 08 '13 16:03

MrQBerrt


People also ask

How does Gradle run JUnit tests?

Test detection By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.

Does Gradle use JUnit?

The junit-jupiter dependency is an aggregator artifact which simplifies the dependency management because it ensures that the required dependencies are found from the classpath. Gradle has a native support for JUnit 5, but this support isn't enabled by default.


1 Answers

This should do the trick:

sourceSets {
    main {
        java {
            srcDirs = ["some/path"]
            exclude "**/*Test.java"
        }
    }
    test {
        java {
            srcDirs = ["some/path"]
            include "**/*Test.java"
        }
    }
}
like image 119
Peter Niederwieser Avatar answered Oct 13 '22 00:10

Peter Niederwieser