Gradle equivalent of Surefire classpathDependencyExclude - java

I'm trying to migrate java project from maven to gradle. The problem is very tricky classpath dependency configuration for tests now.
Our maven-surefire-plugin configuration:
<includes>
<include>**/SomeTest1.java</include>
</includes>
<classpathDependencyExcludes>
<classpathDependencyExclude>com.sun.jersey:jersey-core</classpathDependencyExclude>
</classpathDependencyExcludes>
There are different classpathes for different test-classes. How can I implement it with Gradle?

First of all you need to differ your tests sources into separate sourceSets. Say, we need to run tests from package org.foo.test.rest with a little different runtime classpath, than other tests. Thus, its execution will go to otherTest, where remain tests are in test:
sourceSets {
otherTest {
java {
srcDir 'src/test/java'
include 'org/foo/test/rest/**'
}
resources {
srcDir 'src/test/java'
}
}
test {
java {
srcDir 'src/test/java'
exclude 'org/foo/rest/test/**'
}
resources {
srcDir 'src/test/java'
}
}
}
After that, you should make sure that otherTest has all required compile and runtime classpaths set correctly:
otherTestCompile sourceSets.main.output
otherTestCompile configurations.testCompile
otherTestCompile sourceSets.test.output
otherTestRuntime configurations.testRuntime + configurations.testCompile
The last thing is to exclude (or include) unneeded runtime bundles from test:
configurations {
testRuntime {
exclude group: 'org.conflicting.library'
}
}
And to create Test Gradle task for otherTest:
task otherTest(type: Test) {
testClassesDir = sourceSets.otherTest.output.classesDir
classpath += sourceSets.otherTest.runtimeClasspath
}
check.dependsOn otherTest

Use next workaround:
Create source set for needed tests
Add configurations for created sourceSet
Add task for run test with custom configuration
Configure test task dependOn customized test task
Configure Report plugin for generate beautiful html report :)
Like this getting started

Related

Integration tests in Gradle using Maven's naming conventions?

Coming from Maven, I'm exploring Gradle as an alternative. Technologies: Java 11, jUnit 5, Maven 3.6, Gradle 5.6.
I'm stuck in configuring the integration tests. Following the default naming conventions of Maven's Surefire and Failsafe plugins, my tests live in the standard test directory and are distinguished by their suffix: unit tests end in Test.java and integration tests end in IT.java.
Is it possible to have the same setup in Gradle? So far I've seen two ways:
use jUnit5's tags (which means I would have to go and tag every integration test)
use separate directories for unit and integration tests
Ideally, I'd like to keep my folder structure as-is, because it affects multiple git repositories.
Okay, I think I managed to get it working with source sets, thanks to the feedback of Lukas and Slaw.
Please let me know if this can be improved:
// define the dependencies of integrationTest, inherit from the unit test dependencies
configurations {
integrationTestImplementation.extendsFrom(testImplementation)
integrationTestRuntimeOnly.extendsFrom(testRuntimeOnly)
}
sourceSets {
test {
java {
// exclude integration tests from the default test source set
exclude "**/*IT.java"
}
}
// new source set for integration tests
integrationTest {
// uses the main application code
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
java {
// the tests are at the same directory (or directories) as the unit tests
srcDirs = sourceSets.test.java.srcDirs
// exclude the unit tests from the integration tests source set
exclude "**/*Test.java"
}
resources {
// same resources directory (or directories) with the unit tests
srcDirs = sourceSets.test.resources.srcDirs
}
}
}
// define the task for integration tests
task integrationTest(type: Test) {
description = "Runs integration tests."
group = "verification"
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
shouldRunAfter test
// I'm using jUnit 5
useJUnitPlatform()
}
// make the check task depend on the integration tests
check.dependsOn integrationTest

Gradle (java): test task should use generated .jar and not the .class files in classpath

Gradle with apply plugin: 'java' in build.gradle. The file will create a .jar file and the test task is running junit tests:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
This is working. But to make sure the public API tests are working with the generated .jar file I want that the 'test' task is running the test with the generated .jar file from the build/libs folder in classpath and not with the generate .class files from folder build/classes in in classpath.
Not working because the sourceSets is set global:
tasks.withType(Test) {
sourceSets {
main {
java {
exclude '**'
}
}
}
}
Partly working: multiproject (test and jar separated in two gradle projects):
dependencies {
compile project(":jar_project")
testCompile 'junit:junit:4.12'
}
in this case jar_project.jar is used but package private test are also executed without an error.
Do somebody have an idea how to run the tests with the .jar as dependency and ignoring the .class files?
Thank you, pulp
The problem is that the test task does not depend on the jar task, which sort of makes sense because the jar is supposed to package the classes, so there should be no reason for tests to depend on that jar.
You can force the dependency by:
test {
dependsOn jar
doFirst {
classpath += jar.outputs.files
}
}
Now the jar will be on the test classpath

Adding integration tests to Kotlin project using the Kotlin Gradle DSL

I would like to add an additional "source set" to a Kotlin project that will contain integration tests. I have seen a few posts that talk about doing it for either a vanilla Java project or for Kotlin but using Groovy rather than the Kotlin Gradle DSL.
In summary, using the Kotlin Gradle DSL:
how to add an additional "source set" that can contain Kotlin code, Java code & resources for the purpose of separating integration tests from regular unit tests?
how to add an additional task and configuration to run the integration tests separately from unit tests?
I would expect the directory structure to look something like:
src
main
java
kotlin
resources
test
java
kotlin
resources
integration
java
kotlin
resources
Related:
https://ryanharrison.co.uk/2018/07/25/kotlin-add-integration-test-module.html
https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/
How do I add a new sourceset to Gradle?
Thanks
First, create source set and configuration:
sourceSets {
create("intTest") {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
}
}
val intTestImplementation: Configuration by configurations.getting {
extendsFrom(configurations.implementation.get())
}
val intTestRuntimeOnly: Configuration by configurations.getting {
extendsFrom(configurations.runtimeOnly.get())
}
And then, create the task to run them:
val integrationTest = task<Test>("integrationTest") {
description = "Runs integration tests"
group = "verification"
testClassesDirs = sourceSets["intTest"].output.classesDirs
classpath = sourceSets["intTest"].runtimeClasspath
shouldRunAfter("test")
}
Also, you can add dependencies to be used by the new source set. For instance:
intTestImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
intTestRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
You must add following configuration to your build.gradle file
configurations {
integrationTestImplementation.extendsFrom implementation
integrationTestRuntimeOnly.extendsFrom runtimeOnly
}
dependencies {
intTestImplementation 'junit:junit:4.12'
...
}
sourceSets {
integrationTest {
kotlin {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integrationTest/kotlin')
}
}
}
task integrationTest(type: Test, dependsOn: []) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
useJUnitPlatform()
}
source:
https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests

Running tests from JAR dependency

I'm working on creating a JAR with my test code and planning to run the test from a different project.
Below is the approach I'm trying to implement.
Project B:
configurations { testApi }
task testJar ....
artifacts { testApi testJar }
Project A:
dependencies {
testRuntime "xx.xxx.xxx.projectName", configuration: "integtest";
}
The test seems to be not running with this approach. Any idea on what could be the issue? Any better approaches?
Gradle only looks for test classes in the directories configured via testClassesDirs property of the respective Test-type task which with the java plugin is project.sourceSets.test.output.classesDirs for the test task. Running tests from JAR files in dependencies is not supported.
You need to extract the JAR file and add the extracted directory to this property. I think something like the following construct should work, I didn't test it though:
configurations {
externalTests
}
dependencies {
externalTests "xx.xxx.xxx.projectName", configuration: "integtest"
testRuntime "xx.xxx.xxx.projectName", configuration: "integtest"
}
test {
// if only one dependency in externalTests you can use the simpler, it will fail if there are multiple dependencies
testClassesDirs += zipTree(configurations.externalTests.singleFile)
// if multiple dependencies in externalTests you need to use
testClassesDirs += configurations.externalTests.files.collect { zipTree it }.sum()
}

How to run JUnit tests with Gradle?

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?
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.
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.
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.
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")
// ...
}
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
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

Categories