IntelliJ Idea, Gradle Junit Test - Output Shows Blank Lines - java

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

Related

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?

how to log resulting files from Gradle build task

I'm trying to write a BitBucket Pipeline script for my repository, but so far without much luck, because Gradle seems a pain to debug.
Is there any way to show the resulting files (from compiling the jar for example) in the console when it finishes the : build task?
Preferably I'd like to see as much information as possible, rather too much than too little.
Adding logging.captureStandardOutput LogLevel.DEBUG in my build.gradle file didn't seem to do much, still getting the same output:
:compileJavaNote: -snip-\src\main\java\com\-snip-\atlas\utility\SchematicUtil.java uses or overrides a deprecated API.
Note: Recompile with -Xlint: deprecation for details.
:processResources
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 1.16 secs
Here's one way to do it (e.g. jar for a simple Java project). The build.gradle is:
apply plugin: 'java'
jar {
doLast {
new File("${buildDir}/libs").eachFileRecurse { file ->
println "TRACER: ${file.getAbsolutePath()}"
}
}
}
It should be straight-forward to tailor for other needs.

How to list classpath for tests in Gradle

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) }
}

Followup to Gradle 1.3: build.gradle not building classes

I am actually using the newly released Gradle 2, but having the same issues as described in the previous post.
I am also a newb trying to follow the example given in the Spring guide (http://spring.io/guides/gs/gradle/#scratch) but after my first compile, there were no classes.
I have tried various configurations of tree structure including adding the structure and code suggested in the above thread:
"I guess the source file path is src/org/gradle/example/simple/HelloWorld.java. (The diagram shows something different.) That doesn't match Gradle's default, so you'll have to reconfigure it: sourceSets.main.java.srcDirs = ["src"] – Peter Niederwieser Dec 7 '12 at 1:23 "
adding the line: sourceSets.main.java.srcDirs = ["src"] allows the code to compile, however, I still have no classes.
Here is the successful build.
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
BUILD SUCCESSFUL
Total time: 4.468 secs
Here is the build file:
apply plugin: 'java'
sourceSets.main.java.srcDirs=["src"]
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile "joda-time:joda-time:2.2"
}
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
task wrapper(type:Wrapper) {
gradleVersion = '1.11'
}
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
Where are my classes? Please help.
After I got stuck with the same problem, I hacked around for a bit before I understood the reason for this behavior.
My project structure was like so
MyProject
- src
- main
- java
- hello
HelloWorld.java
build.gradle
The problem was that the build.gradle is supposed to be under the Project-Root folder i.e. MyProject and not under the hello folder !!!
Changed it so that my Project structure looks like below, ran the gradle build and saw that classes folder was created:
MyProject
- src
- main
- java
- hello
HelloWorld.java
build.gradle
When you think about it, the build.gradle is used to build the complete project and not just the classes within one folder and should rightfully sit under the project-root folder.

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