Reputation: 177
I can't manage to use gradle to get JUnit5 tests to work, compileJava failes with a message that it can't find org.junit.jupiter.api, despite me having put it in the dependencies.
I have to compile some code that was give to me. It has a directory structure like this:
├── build.gradle
├── src
│ ├── frame
│ │ ├── CardTestfileReader.java
│ │ ├── PublicTests.java
│ │ └── SortArray.java
│ └── lab
│ ├── Card.java
│ ├── HybridSort.java
│ ├── HybridSortRandomPivot.java
│ └── YourTests.java
└── tests
└── public
├── TestFile1
├── TestFile2
└── TestFile3
The JUnit5 tests are in frame.PublicTests, I need to run those.
I managed to copypasta this build.groovy
file without really understanding what it is doing.
plugins {
id 'java'
id 'checkstyle'
}
repositories {
mavenCentral()
}
dependencies {
testCompile('org.junit.jupiter:junit-jupiter:5.4.2')
testImplementation('org.junit.jupiter:junit-jupiter:5.4.2')
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
sourceSets {
main {
java {
srcDir 'src'
}
}
test {
java {
srcDir 'src'
}
}
}
I'm hoping to get gradle test
to work and run the unit tests somehow.
Upvotes: 3
Views: 12902
Reputation: 27986
You have this strange setup where both "main" and "test" source sets reference the same "src" folder.
So, PublicTests.java
is actually in two source sets (both main and test). So there's actually two compile tasks which point to the exact same sources. The "main" source set is failing to compile because junit is not on your compile classpath.
* DO NOT PUT JUNIT ON THE COMPILE CLASSPATH TO FIX THIS *
Your build is messed up and you need to separate your tests from your main sources, and stop sharing the "src" folder with both source sets.
Do yourself a favour and stick to Gradle's sensible defaults. Put your application code in src/main/java
and put your tests in src/test/java
Upvotes: 4
Reputation: 2837
With Gradle, the same project can have multiple configurations (in example "main" and "test"), and each configuration may have a different classpath. You are importing the junit5
dependencies using the directive testCompile('org.junit...')
; but that includes the junit5 jar in the test configuration only.
Note that the PublicTests
class is placed under main
, so Gradle will compile this under the "main" configuration. If PublicTests
references junit5, then this will cause the compilation error (since junit5 is not included in the "main" classpath).
Long story short.. You should put all your tests under test
.
You can check the classpath for every configuration by calling gradle dependencies
.
Upvotes: 3