How to list classpath for tests in Gradle - java

When I try running gradle test, I get the following output:
$ gradle test
:ro:compileJava UP-TO-DATE
:ro:processResources UP-TO-DATE
:ro:classes UP-TO-DATE
:ro:jar
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
ro.idea.ToggleTest > testIsAd FAILED
java.lang.NoClassDefFoundError at ToggleTest.java:13
Caused by: java.lang.ClassNotFoundException at ToggleTest.java:13
ro.idea.ToggleTest > testToggle FAILED
java.lang.NoClassDefFoundError at ToggleTest.java:13
2 tests completed, 2 failed
:test FAILED
So I want to check my classpath to see whether my classpath is wrong or not.
My question is: How can I list the classpath at test time with a Gradle task?

You can list test runtime dependencies with:
gradle dependencies --configuration=testRuntime
Or, if you want to see the actual files:
task printClasspath {
doLast {
configurations.testRuntime.each { println it }
}
}

Also, to list the classpath for the main (non-test) application run use:
run << {
doLast {
configurations.runtime.each { println it }
}
}

Worked for me (Gradle 6.3, Kotlin DSL):
tasks.withType<Test> {
this.classpath.forEach { println(it) }
}

this works for me (in Gradle 5.6)
task printTestClasspath.doLast {
println(sourceSets.test.runtimeClasspath.asPath)
}

Assuming you are using a Gradle wrapper, you can use the following.
./gradlew dependencies --configuration=testRuntimeClasspath
It will list the dependencies as available to your tests.

Expanding solution of Peter Niederwieser, if you want to print from all possible configurations (using the simple loop) and ignoring possible errors:
task printClasspath {
doLast {
configurations.each { Configuration configuration ->
try {
println configuration
configuration.each { println it }
} catch (Exception e){
println "Error getting details of $configuration"
}
}
}
}

I'm using gradle 7.3.3. and this worked for me:
tasks.withType<Test> {
useJUnitPlatform()
println("Test classpath")
sourceSets.test.get().runtimeClasspath.forEach { println(it) }
}

Related

IntelliJ Idea, Gradle Junit Test - Output Shows Blank Lines

I am running Idea 2020.1.3 Ultimate.
I have created a Gradle project with Gradle 6.5.1 and Junit 5.6.2.
When running a simple test that only prints output to the screen, I see that after each line, Idea adds a blank line. This is making analyzing the results very difficult, since it doubles and triples the output.
This issue doesn't restore when running Gradle from command line.
It does restore when running with TestNG instead of JUnit.
It was also restored with JUnit 4 versions.
Any ideas?
Test Code:
#Test
public void testDummy(){
System.out.println("line1");
System.out.println("line2");
System.out.println("line3");
}
Test output (The blank lines are from the output):
Output:
Testing started at 13:29 ...
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test
line1
line2
line3
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 839ms
3 actionable tasks: 1 executed, 2 up-to-date
13:29:19: Task execution finished ':test --tests "org.example.tests.DummyTest.testDummy"'.
build.gradle file:
plugins {
id 'java'
id 'idea'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.2'
}
test {
useJUnitPlatform()
testLogging {
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
It's a known bug. The fix is planned for 2020.3.
I ran your tests from the console and everything was correct. Looks like IntelliJ is adding these blank lines to your output. If you want to get rid of them, consider running your tests from the console (or the IntelliJ terminal).
Anyway, I found a way to trick IntelliJ to stop adding blank lines to your output. Instead of using System.out.println, use System.out.print and then flush the PrintStream. You should use a simple function to do this, like:
private void log(String message) {
System.out.print(message);
System.out.flush();
}
and then invoke
log("line1");
log("line2");
log("line3");
which produces the following output:
...
> Task :test
line1
line2
line3

Gradle test filter doesn't work for complex test task

I have a project with unit tests.
We decided to migrate from testng to JUnit 5 but do it in a couple of iterations.
And now in our project tests for both platforms are present.
I handle it using the following build script:
def commonTestConfig =
{
ignoreFailures = true
...
}
task testNGTests(type: Test) {
useTestNG()
configure commonTestConfig
reports.html.destination = file("$buildDir/reports/testng")
}
task junitTests(type: Test) {
useJUnitPlatform()
configure commonTestConfig
reports.html.destination = file("$buildDir/reports/junit")
}
test {
dependsOn testNGTests
dependsOn junitTests
}
But if I try to run specific test from command line then build fails:
call gradle --warning-mode all test --tests my.package.LicenseTests
Task :compileJava UP-TO-DATE
Task :processResources UP-TO-DATE
Task :classes UP-TO-DATE
Task :compileTestJava UP-TO-DATE
Task :processTestResources UP-TO-DATE
Task :testClasses UP-TO-DATE
Task :testNGTests UP-TO-DATE
Task :junitTests UP-TO-DATE
Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong: Execution failed for task ':test'.
No tests found for given includes: [my.package.LicenseTests](--tests filter)
I.e. the command above runs all tests(tasks testNGTests and junitTests) without applying provided filter.
And after that tries to find tests to run in test task where no any one are supposed to run.
Is there a way to run both JUnit 5 and testng tests in Gradle cleanly, in a way preserving possibility to pass test filter from command line?

Make clean task before any other

I would like to make releaseBuild task that will execute clean task before building project.
I have prepared script that works great to do clean before releaseBuild task, not cleaning just before finishing thanks to jar.mustRunAfter(clean).
However, I would like also to have possibility to execute publishToMavenLocal task that will be done only after releaseBuild finish successfully (so cleaning and building). Then there is problem.
plugins {
id 'java'
id 'maven-publish'
}
group 'com.example'
version '1.0-SNAPSHOT'
task releaseBuild {
outputs.upToDateWhen { false }
// generatePomFileForMavenJavaPublication.mustRunAfter(clean)
jar.mustRunAfter(clean)
releaseBuild.dependsOn(clean, build)
}
publishToMavenLocal.dependsOn(releaseBuild)
publishing {
publications {
mavenJava(MavenPublication) {
pom {
from components.java
packaging 'jar'
}
}
}
}
gradle releaseBuild works well.
gradle publishToMavenLocal shows the error:
> Task :generatePomFileForMavenJavaPublication
> Task :clean
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar
> Task :publishMavenJavaPublicationToMavenLocal FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':publishMavenJavaPublicationToMavenLocal'.
> Failed to publish publication 'mavenJava' to repository 'mavenLocal'
> java.io.FileNotFoundException: __path__\build\publications\mavenJava\pom-default.xml (System nie może odnaleźć określonej ścieżki -- File not found)
The clean is after generating pom file.
When I uncomment line with generatePomFileForMavenJavaPublication.mustRunAfter(clean), I've got error:
> Could not get unknown property 'generatePomFileForMavenJavaPublication' for task ':releaseBuild' of type org.gradle.api.DefaultTask.
Add at the end of your script.
publishMavenJavaPublicationToMavenLocal.mustRunAfter(clean)
There is a task-tree plugin which is very helpful in such situations.
It is handy to distinguish between Configuration and Execution phases of Gradle build when fine-tuning tasks configuration. It will help if you read about Build Lifecycle.
Adding dependencies to the clean task usually gives more headache than benefit. Consider adding only "soft" dependencies like mustRunAfter and add clean task explicitly to your command line:
gradle clean releaseBuild publishToMavenLocal

Running integration test in Gradle using junit

I am using Gradle to run my integration test. I am using JUnit for tests. Integration tests are not being executed when a Gradle task is being triggered.
I am using JUnit version 4.12 and Gradle version 3.5.
I am migrating a project from Ant build to Gradle. it executes all the integration tests when ant task is being triggered. I need to replace the build.xml file by build.gradle to execute all the integration test. This is the code in build.gradle file
apply plugin: 'java'
repositories {
mavenCentral()
}
sourceSets {
test {
java {
srcDirs = ['src/main/java']
}
resources {
srcDirs = ['src/test']
}
}
}
dependencies {
testCompile 'junit:junit:4.12'
testRuntimeOnly 'junit:junit:4.12'
compileClasspath sourceSets.main.output + sourceSets.test.output
runtimeClasspath sourceSets.main.output + sourceSets.test.output
}
task run(type: Test) {
useJUnit()
}
I am using another build.gradle file to copy the resources and code dependency to build/classes folder. code for this is:
distributions {
distZip.enabled = false
main {
baseName = 'project name'
version = ''
contents {
from { 'build.gradle' }
from { '../config/gradle/common-build.gradle' }
from {'../gradle*'}
from { 'build/libs/*.jar' }
into('build/classes') {
from('build/classes')
}
into('build/resources') {
from('build/resources')
}
}
}
}
i am using apply to import one build.gradle file into another using this code:
apply from: 'buildFileLocation/build.gradle'
But whenever I execute the task I am getting this as output:
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :run NO-SOURCE

Cucumber app with gradle runs fine locally but fails on Jenkins

When i run my cucumber based project it behaves as though it cannot find the Steps classes - I can tell because the log on Jenkins is offering code snippets for implementation.
I'm using Gradle and I know there are issues with cucumber-jvm.
The relevant part of the build file:
task cucumber(type: JavaExec) {
dependsOn assemble, compileTestJava
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['-f', 'pretty', '--glue', 'steps', 'src/test/resources']
systemProperties = System.getProperties()
}
I can't see why running the same command (./gradlew clean cucumber) via Jenkins fails?
Here's the output from Jenkins:
+ ./gradlew -Dapk=/Users/rakesh/workspace1/AcmeApp_4_10_Tiles/bin/AcmeApp_4_10_Tiles.apk clean cucumber
:clean
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble
:compileTestJava
:cucumber
Feature: Thrid party request for the Landing page.
TODO: additional requests for box connection
...
You can implement missing steps with the snippets below:
#Given("^the user chooses not to login$")
...
BUILD SUCCESSFUL
Total time: 8.074 secs
$ /Users/rakesh/Apps/adt-bundle-mac-x86_64-20131030/sdk/platform-tools/adb disconnect emulator-5812
[android] Stopping Android emulator
[android] Archiving emulator log
$ /Users/rakesh/Apps/adt-bundle-mac-x86_64-20131030/sdk/platform-tools/adb kill-server
Finished: SUCCESS
I would also like to mention that sometimes this problem occurs,if there are dependencies missing in build.gradle.
In my case,the following statement was missing in main task in build.gradle..
dependsOn assemble, compileTestJava
Adding this,resolved the issue.
I solved the problem and it had nothing to do with cucumber, gradle or Jenkins! I had renamed the steps classes (case change) which completely confused subversion and the java step classes were actually missing!!

Categories

Resources