Running tests from Gradle dependency without extending them - java

We have two Spring Boot projects, and I'd like to run the integration tests from one project in the other project without extending those test files.
Project Lib is a library file of common code. We're including that as a dependency in Project Impl. We're publishing the test sources from Lib with the 'tests' classifier and including that in Impl. We can extend the tests from Lib in Impl and it will get picked up, but we'd like to have those tests run automatically without needing to extend.
Gradle 7.5.1
Project Lib:
task testJar(type : Jar) {
from sourceSets.test.output
archiveClassifier = "tests"
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId groupId
artifactId 'artifact-lib'
from components.java
artifact sourceJar
artifact testJar
}
}
}
test file in src/test/java/package.integration
#ContextConfiguration
#ActiveProfiles({"test"})
#ExtendWith({SpringExtension.class})
public class LibTest {
[...]
}
Project Impl:
dependencies {
testImplementation 'group:artifact-lib:0.0.+:tests'
}
tasks.withType(Test).configureEach {
useJUnitPlatform()
}
test {
exclude "**/integration/**"
}
task intTest(type: Test, dependsOn: buildDocker) {
include "**/integration/**"
}
Works if we extend the lib test:
#ExtendWith(SpringExtension.class)
public class ImpleTest extends LibTest {
}
I did see this answer from years ago, but I was hoping there was a simpler way these days:
Gradle to run tests from "test" dependency jars
Thanks!

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

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 do you compile a Gradle sub-project using debug classes from another module

I am working on the evolution of an Apache Ant project to compile it with Gradle, and I have different set of sub-projects.
My goal is to use the output of the main project as a dependency of some of the subprojects. The global configuration is the following:
MainProject:
-> src/main/java
-> src/main/resources
-> src/test/java
-> src/test/resources
-> src/mocks/java
ChildProject1:
-> src/main/java
-> src/main/resources
-> src/test/java
-> src/test/resources
-> src/mocks/java
The main project is correctly included in the child build path and all links are correctly found when it comes to Eclipse linking.
I have included the main project in the settings.gradle file for the ChildProject and its dependencies in the build:
dependencies {
compile project(':MainProject')
}
My problem is that the ChildProject needs outputs from the mocks/java to compile and though it seems that the Gradle compilation process does include the "default" output that are the main/java files, I get an error message saying that "symbols" from the mocks can not be found.
I had a similar problem with the MainProject test java files compilation that required the mocks to be used, it was solved by adding:
sourceSets {
mocks {
java {
srcDir 'src/mocks/java'
}
compileClasspath += sourceSets.main.runtimeClasspath
}
test {
compileClasspath += sourceSets.mocks.output
runtimeClasspath += sourceSets.mocks.output
}
}
The mock source is correctly added to the test classpath.
How do I tell my ChilProject to use the mocks output from MainProject to compile its tests?
I'll try to point you in correct direction. Normally you declare multi-project dependencies in Gradle like so:
dependencies {
compile project(':projA')
}
This define implicitly says use the default artifacts of projA as a dependency.
I believe what you want instead is to use the mock artifact of your projA dependency. The way to do this is:
Create a new jar task to combine the class files compiled from the main and mock sourceSet.
task mocksJar(type: Jar) {
classifier 'mocks'
group 'build'
from sourceSets.main.output
from sourceSets.mocks.output
}
Export the mock artifact in projA build by creating a new Gradle configuration called mocks and assigning the artifact to export:
configuration { mocks }
artifacts.mocks mocksJar
Import the mocks configuration in your project that requires projA using the extended dependency syntax:
dependencies {
compile project( path:':projA', configuration:'mocks')
}

Gradle equivalent of Surefire classpathDependencyExclude

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

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.
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.
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
Simple way is to add explicit task dependency in ProjectB:
compileTestJava.dependsOn tasks.getByPath(':ProjectA:testClasses')
Difficult (but more clear) way is to create additional artifact configuration for ProjectA:
task myTestsJar(type: Jar) {
// pack whatever you need...
}
configurations {
testArtifacts
}
artifacts {
testArtifacts myTestsJar
}
and add the testCompile dependency for ProjectB
apply plugin: 'java'
dependencies {
compile project(':ProjectA')
testCompile project(path: ':ProjectA', configuration: 'testArtifacts')
}
I've come across this problem myself recently, and man is this a tough issues to find answers for.
The mistake you are making is thinking that a project should export its test elements in the same way that it exports its primary artifacts and dependencies.
What I had a lot more success with personally was making a new project in Gradle. In your example, I would name it
Project A_Test
-> src/main/java
I would put into the src/main/java the files that you currently have in Project A/src/test/java. Make any testCompile dependencies of your Project A compile dependencies of Project A_Test.
Then make Project A_Test a testCompile dependency of Project B.
It's not logical when you come at it from the perspective of the author of both projects, but I think it makes a lot of sense when you think about projects like junit and scalatest (and others. Even though those frameworks are testing-related, they are not considered part of the "test" targets within their own frameworks - they produce primary artifacts that other projects just happen to use within their test configuration. You just want to follow that same pattern.
Trying to do the other answers listed here did not work for me personally (using Gradle 1.9), but I've found that the pattern I describe here is a cleaner solution anyway.
I know it's an old question but I just had the same problem and spent some time figuring out what is going on. I'm using Gradle 1.9. All changes should be in ProjectB's build.gradle
To use test classes from ProjectA in tests of ProjectB:
testCompile files(project(':ProjectA').sourceSets.test.output.classesDir)
To make sure that sourceSets property is available for ProjectA:
evaluationDependsOn(':ProjectA')
To make sure test classes from ProjectA are actually there, when you compile ProjectB:
compileTestJava.dependsOn tasks.getByPath(':ProjectA:testClasses')
Please read the update bellow.
Similar problems described by JustACluelessNewbie occurs in IntelliJ IDEA. Problem is that dependency testCompile project(':core').sourceSets.test.output actually means: "depend on classes generated by gradle build task". So if you open clean project where classes are not generated yet IDEA won't recognise them and reports error.
To fix this problem you have to add a dependency on test source files next to dependency on compiled classes.
// First dependency is for IDEA
testCompileOnly files { project(':core').sourceSets.test.java.srcDirs }
// Second is for Gradle
testCompile project(':core').sourceSets.test.output
You can observe dependencies recognised by IDEA in Module Settings -> Dependencies (test scope).
Btw. this is not nice solution so refactoring is worth considering. Gradle itself does have special subproject containing test-support classes only. See https://docs.gradle.org/current/userguide/test_kit.html
Update 2016-06-05
More I am thinking about proposed solution less I like it. There are few problems with it:
It creates two dependencies in IDEA. One points to test sources another to compiled classes. And it is crucial in which order these dependencies are recognised by IDEA. You can play with it by changing dependency order in Module settings -> Dependencies tab.
By declaring these dependencies you are unnecessarily polluting dependency structure.
So what's the better solution? In my opinion it's creating new custom source set and putting shared classes into it. Actually authors of Gradle project did it by creating testFixtures source set.
To do it you just have to:
Create source set and add necessary configurations. Check this script plugin used in Gradle project: https://github.com/gradle/gradle/blob/v4.0.0/gradle/testFixtures.gradle
Declare proper dependency in dependent project:
dependencies {
testCompile project(path: ':module-with-shared-classes', configuration: 'testFixturesUsageCompile')
}
Import Gradle project to IDEA and use the "create separate module per source set" option while importing.
New testJar based (trnsitive dependancies supported) solution available as gradle plugin:
https://github.com/hauner/gradle-plugins/tree/master/jartest
https://plugins.gradle.org/plugin/com.github.hauner.jarTest/1.0
From documentation
In case you have a multi-project gradle build you may have test
dependencies between sub-projects (which probably is a hint that your
projects are not well structured).
For example assume a project where the sub-project Project B depends
on Project A and B does not only have a compile dependency on A but
also a test dependency. To compile and run the tests of B we need some
test helper classes from A.
By default gradle does not create a jar artifact from the test build
output of a project.
This plugin adds a testArchives configuration (based on testCompile)
and a jarTest task to create a jar from the test source set (with the
classifier test added to name of the jar). We can then depend in B on
the testArchives configuration of A (which will also include the
transitive dependencies of A).
In A we would add the plugin to build.gradle:
apply plugin: 'com.github.hauner.jarTest'
In B we reference the
testArchives configuration like this:
dependencies {
...
testCompile project (path: ':ProjectA', configuration: 'testArchives')
}
The Fesler's solution haven't worked for me, when i tried it to build an android project (gradle 2.2.0).
So i had to reference required classes manually :
android {
sourceSets {
androidTest {
java.srcDir project(':A').file("src/androidTest/java")
}
test {
java.srcDir project(':A').file("src/test/java")
}
}
}
Here if you are using Kotlin DSL, you should create your task like that according to Gradle documentation.
Like some previous answer, you need to create a special configuration inside the project that will share its tests class, so that you don't mix test and main classes.
Simple steps
In project A you would need to add in your build.gradle.kts :
configurations {
create("test")
}
tasks.register<Jar>("testArchive") {
archiveBaseName.set("ProjectA-test")
from(project.the<SourceSetContainer>()["test"].output)
}
artifacts {
add("test", tasks["testArchive"])
}
Then in your project B in the dependencies, you will need to add in your build.gradle.kts:
dependencies {
implementation(project(":ProjectA"))
testImplementation(project(":ProjectA", "test"))
}
I'm so late to the party (it is now Gradle v4.4) but for anyone else who finds this:
Assuming:
~/allProjects
|
|-/ProjectA/module-a/src/test/java
|
|-/ProjectB/module-b/src/test/java
Go to the build.gradle of project B (the one that needs some test classes from A) and add the following:
sourceSets {
String sharedTestDir = "${projectDir}"+'/module-b/src/test/java'
test {
java.srcDir sharedTestDir
}
}
or (assuming your project is named ProjectB)
sourceSets {
String sharedTestDir = project(':ProjectB').file("module-b/src/test/java")
test {
java.srcDir sharedTestDir
}
}
Voila!
Creating test-jar For Gradle 6.6.x
I know that there are many sources telling you, that is not OK, fe:
https://github.com/gradle/gradle/issues/11280
https://gradle.org/whats-new/gradle-6/#better-builds
But this is so damn simple and I just don't like the idea of having common test classes separately in testFixtures folder.
So in module A:
task jarTests(type: Jar, dependsOn: testClasses) {
classifier = 'tests'
from sourceSets.test.output
}
configurations {
tests {
extendsFrom testRuntime
}
}
artifacts {
tests jarTests
}
And in module B:
testImplementation project(':moduleA')
testImplementation project(path: ':moduleA', configuration: 'tests')
And it just works!
If you want to use artifact dependencies to have:
ProjectB's source classes depend on Project A's source classes
ProjectB's test classes depend on Project A's test classes
then ProjectB's dependencies section in build.gradle should look something like this:
dependencies {
compile("com.example:projecta:1.0.0")
testCompile("com.example:projecta:1.0.0:tests")
}
For this to work ProjectA needs to build a -tests jar and include it in the artifacts it produces.
ProjectA's build.gradle should contain configuration like this:
task testsJar(type: Jar, dependsOn: testClasses) {
classifier = 'tests'
from sourceSets.test.output
}
configurations {
tests
}
artifacts {
tests testsJar
archives testsJar
}
jar.finalizedBy(testsJar)
When ProjectA's artifacts are published to your artifactory they will include a -tests jar.
The testCompile in ProjectB's dependencies section will bring in the classes in the -tests jar.
If you want to includeFlat ProjectA's source and test classes in ProjectB for development purposes then the dependencies section in ProjectB's build.gradle would look like this:
dependencies {
compile project(':projecta')
testCompile project(path: ':projecta', configuration: 'tests')
}
If you have mock dependencies which you need to share between tests, you can create new project projectA-mock and then add it as test dependency to ProjectA and ProjectB:
dependencies {
testCompile project(':projectA-mock')
}
This is clear solution to share mock dependencies, but if you need to run tests from ProjectA in ProjectB use other solution.
The solution mentioned by Nikita for Android + Kotlin looks like this:
task jarTests(type: Jar, dependsOn: "assembleDebugUnitTest") {
getArchiveClassifier().set('tests')
from "$buildDir/tmp/kotlin-classes/debugUnitTest"
}
configurations {
unitTestArtifact
}
artifacts {
unitTestArtifact jarTests
}
Gradle for project that is going to use dependencies:
testImplementation project(path: ':shared', configuration: 'unitTestArtifact')
If you are struggling to adapt the solution to the Gradle Kotlin DSL this is the equivalent:
configurations {
register("testClasses") {
extendsFrom(testImplementation.get())
}
}
val testJar = tasks.register<Jar>("testJar") {
archiveClassifier.set("test")
from(sourceSets.test)
}
artifacts.add("testClasses", testJar)
Some of the other answers caused errors one way or another - Gradle did not detect test classes from other projects or Eclipse project had invalid dependencies when imported. If anyone has the same problem, I suggest going with:
testCompile project(':core')
testCompile files(project(':core').sourceSets.test.output.classesDir)
The first line forces the Eclipse to link the other project as dependency, so all sources are included and up to date. The second allows Gradle to actually see the sources, while not causing any invalid dependency errors like testCompile project(':core').sourceSets.test.output does.
in project B:
dependencies {
testCompile project(':projectA').sourceSets.test.output
}
Seems to work in 1.7-rc-2

Categories