Gradle file is not running tests - java

I am trying to run my junit tests through gradle file but build gets successful but does not run any tests. This is how my gradle file looks like:
apply plugin: 'java'
// Creating a new sourceSet because you should move your integration tests to a separate directory.
sourceSets {
test {
java.srcDirs = ['src/integration-test/java']
}
integrationTest {
java.srcDirs = ['src/integration-test/java']
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
}
task integrationTest(type: Test, description: 'Runs the integration tests', group: 'Verification') {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
After running task integrationTest, build runs successfully but it does not run any tests. Does anyone know why?

Don't know what exactly you want to do: separate your integration tests of fully move test classes to other folder but I think you need to include the dependencies of test configuration like this:
configurations {
integrationCompile.extendsFrom testCompile
integrationRuntime.extendsFrom testRuntime
}
For example integration test configuration:
sourceSets {
test {
java.srcDirs = ['src/test/java']
}
integration {
java.srcDirs = ['src/integration/java']
resources.srcDir 'src/integration/resources'
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
}
}
configurations {
integrationCompile.extendsFrom testCompile
integrationRuntime.extendsFrom testRuntime
}
task integration(type: Test, group: 'Verification') {
testClassesDir = sourceSets.integration.output.classesDir
classpath = sourceSets.integration.runtimeClasspath
}
In case if you just want to move your tests in other folder(src/integration-test/java) and run them with test task you can use the following configuration:
sourceSets {
test {
java.srcDirs = ['src/integration-test/java']
}
}

Related

Sonarqube not picking up gradle sourcesets

I have a java gradle project with a custom sourceset “src/integration-test” next to the standard “src/test” folder.
build.gradle
plugins {
...
id 'org.sonarqube' version '3.3'
id 'jacoco'
}
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
jacocoTestReport {
getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec"))
reports {
xml.required = true
}
}
sonarqube {
properties {
properties["sonar.tests"] += sourceSets.integrationTest.allSource.srcDirs.findAll({ it.exists() })
properties["sonar.coverage.jacoco.xmlReportPaths"] = "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"
}
}
...
I’ve tried several things but Sonarqube still only picks up the unit tests in “src/test” and ignoring the ones in my custom sourceset.
Versions:
Gradle v7.3
Sonarqube v3.3
Java 17
What am I doing wrong?
Thanks.

Configuration with name 'integrationTestCompile' not found

when trying to upgrade my gradle version from 5.x to 7.x, getting below error
* What went wrong:
Configuration with name 'integrationTestCompile' not found.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Below is my code
sourceSets {
create("integrationTest") {
compileClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.main.get().output
runtimeClasspath += sourceSets.test.get().output
}
}
val integrationTestCompile: Configuration by configurations.getting {
extendsFrom(configurations.testImplementation.get())
}
val integrationTestRuntimeOnly: Configuration by configurations.getting {
extendsFrom(configurations.testRuntimeOnly.get())
}
tasks {
val integrationTest by creating(Test::class) {
description = "Running integration tests."
group = "verification"
testClassesDirs = sourceSets["integrationTest"].output.classesDirs
classpath = sourceSets["integrationTest"].runtimeClasspath
shouldRunAfter("test")
}
}
Using gradle with kotlin.
How to fix this?
In below way you can create integrationTest task in gradle latest version
configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntimeOnly.extendsFrom runtimeOnly
}
sourceSets {
integrationTest {
compileClasspath += sourceSets.main.output
compileClasspath += configurations['testRuntimeClasspath']
runtimeClasspath += compileClasspath
}
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
mustRunAfter test
}

gradle 6.1 + JUnit 5: integration tests do not generate xml or html reports

I have the following build.gradle in multi-project:
plugins {
id 'idea'
id 'eclipse'
id 'java'
}
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
}
test {
useJUnitPlatform()
minHeapSize = "2048m"
maxHeapSize = "6144m"
reports {
junitXml.enabled = true
html.enabled = true
}
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.6.0'
integrationTestRuntime 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
compile "org.seleniumhq.selenium:selenium-java:3.141.59"
}
task integrationTest(type: Test) {
useJUnitPlatform()
minHeapSize = "10g"
maxHeapSize = "10g"
outputs.upToDateWhen { false }
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = 'Runs the integration tests.'
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
binResultsDir = file("$buildDir/integration-test-results/binary/integrationTest")
reports {
html.enabled = true
junitXml.enabled = true
html.destination = file("$buildDir/integration-test-results")
junitXml.destination = file("$buildDir/integration-test-results")
}
mustRunAfter tasks.test
}
When I run gradle integrationTest, integration tests get invoked, but only binary report is generated in:
build/integration-test-results/binary/integrationTest
No XML or HTML results! Seems that JUnit 5 changed something radically from JUnit 4.
How can I get XML or HTML reports?

Building mutiple Artifacts for Vaadin-Application [Gradle]

I am currently working on a Vaadin 8 project using the gradle vaadin-plugin.
In the project there are 3 SourceSets main,a and b. The later two exclude some views and features. Is it possible to build 3 Artifcats via gradle one for each sourceSet. I tried modifying the war task but that broke the views.
conf{
aCompile.extendsFrom compile
aRuntime.extendsFrom runtime
bCompile.extendsFrom compile
bRuntime.extendsFrom runtime
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/generated']
}
}
a{
java {
srcDirs = ['src/main/java', 'src/main/generated']
exclude 'com/exmaple/features/b'
exclude 'com/exmaple/views/b'
}
compileClasspath += main.output
runtimeClasspath += main.output
output.resourcesDir = 'build/resources/main'
output.classesDir = 'build/classes/java/main'
}
b{
java {
srcDirs = ['src/main/java', 'src/main/generated']
exclude 'com/exmaple/features/a'
exclude 'com/exmaple/views/a'
}
compileClasspath += main.output
runtimeClasspath += main.output
output.resourcesDir = 'build/resources/main'
output.classesDir = 'build/classes/java/main'
}
}
task aWar(type: War) {
appendix = "a"
from sourceSets.a.output
}
task bWar(type: War) {
appendix = "b"
from sourceSets.b.output
}
From https://docs.gradle.org/current/userguide/artifact_management.html#sec:declaring_artifacts you should be able to declare an artifact for every source set.
In your case it would look something like this:
task aWar(type: War, dependsOn: classes) {
baseName = 'a.war'
classpath = project.configurations.aRuntime
}
task bWar(type: War, dependsOn: classes) {
baseName = 'b.war'
classpath = project.configurations.bRuntime
}
artifacts {
archives aWar, bWar
}
The WAR task also allows you to include/exclude classes using a regexp. Checkout more at https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.War.html

gradle integration test not working, unit test running twice

I have this root build.gradle
repositories {
jcenter()
}
subprojects {
apply plugin: 'java'
group 'me.someone'
version '1.0.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
jcenter()
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.12'
}
}
Then I have this child build.gradle
plugins {
id 'java-library'
id 'eclipse'
id "org.springframework.boot" version "2.0.1.RELEASE"
id 'io.spring.dependency-management' version "1.0.5.RELEASE"
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile project(':foo-jar')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.18.3'
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
test {
java.srcDir file('src/int/java')
}
itest {
java {
srcDir file('src/itest/java')
}
//resources.srcDir 'src/itest/resources'
}
}
test {
testLogging {
showStandardStreams = true
events "passed", "skipped", "failed"
exceptionFormat = 'full'
}
}
task itest(type: Test) {
testLogging {
showStandardStreams = true
events "passed", "skipped", "failed"
exceptionFormat = 'full'
}
itest.mustRunAfter test
}
check.dependsOn itest
bootRun {
main = 'me.someone.BarServiceApplication'
}
The issue is unit test runs twice but not the integration test. Why is unit test running twice but not integration test? My understanding is when I provided the source folder of integration test, it should run the integration test as well.
Your task itest needs to have its testClassesDirs configured, that is why it is currently running your unit tests twice. It might also need the classpath configured.
And you should have a look at the Gradle documentation on how to do integration tests for all the details.

Categories

Resources