Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Gradle System.out err print

farenda 2015-06-18 0

Problem:

When running a test with Gradle and trying to print to System.out or System.err nothing happens. How to make Gradle to display standard output?

Solution:

Here’s sample Gradle buildscript:

buildscript {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.codehaus.groovy:groovy-all:2.4.1"
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}

And here’s a sample test class that prints to standard output:

package com.farenda.solved

import spock.lang.Specification

class StandardOutPrintTest extends Specification {

    def 'should tell nothing but the true'() {
        println('Inside the test!')
        expect:
        42 == 42
    }
}

Now, when you run the test from the console, you will see that it prints nothing:

$> gradle test --tests com.farenda.solved.StandardOutPrintTest
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:compileTestGroovy UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE

BUILD SUCCESSFUL

Total time: 7.158 secs

The problem is that Gradle is not logging messages printed to standard out/err, so you have to tell it to do that in your buildscript, using the following command:

buildscript {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin: 'groovy'

repositories {
    mavenCentral()
}

// this turns logging on in tests:
test.testLogging.showStandardStreams = true

dependencies {
    compile "org.codehaus.groovy:groovy-all:2.4.1"
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}

Now, when you rerun the test it will print standard out/err as expected:

$> gradle cleanTest test --tests com.farenda.solved.StandardOutPrintTest
:cleanTest
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:compileTestGroovy UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test

com.farenda.solved.StandardOutPrintTest > should tell nothing but the true STANDARD_OUT
    Inside the test!

BUILD SUCCESSFUL

Total time: 8.684 secs

It works as advertised! :-) As you can see, I had to rerun the test with cleanTest task to make Gradle to actually run the test. It is because when Gradle sees that nothing has changed it does nothing.

Share with the World!
Categories Dev Tools Tags gradle
Previous: Spock Framework before after class
Next: Bash while loop

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored