Make System.out a Mock with Spock Test Framework

Introduction

For this year's training for newcomers, we modified all the Java code + wrote tests and made changes so that the tests would guarantee it. At that time, I also used parameters, and when I made it with Spock, it was a memo. (Junit5 made it easier, but it's still a hassle, so I often use spock)

environment

Java8 Groovy 2.5 spock-core 1.2-groovy-2.5 Gradle4.10

This phenomenon

I was creating a test to check what was displayed by converting Java System.out to Mock with spock. At that time, I was hit by a phenomenon that an error occurred at startup if I wrote it smoothly and moved it.

Code in question

Check in Hello World to reproduce the phenomenon. The code of the main body and the test code are as follows.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
class HelloWorldTest extends Specification {
    def mainTest() {
        setup:
        PrintStream printStream = Mock()
        System.out = printStream

        when:
        HelloWorld.main(null)

        then:
        1 * printStream.println("Hello World")
    }
}

solution

It's OK if you add cglib-nodep and objenesis as Dependency (the error also says so). So, add it and move it, and you're done. The final working .build.gradle is as follows.

plugins {
    id 'java'
    id 'groovy'
}

group 'com.tasogarei'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile "org.codehaus.groovy:groovy:2.5.6"
    testCompile "org.spockframework:spock-core:1.2-groovy-2.5"
    testCompile "cglib:cglib-nodep:3.2.10"
    testCompile "org.objenesis:objenesis:3.0.1"
}

Recommended Posts

Make System.out a Mock with Spock Test Framework
Make a digging maze with Ruby2D
Make a slideshow tool with JavaFX
Make a Christmas tree with swift
Make a list map with LazyMap
Mock only some methods with Spock
Test Spring framework controller with Junit
Make a typing game with ruby
Let's make a Christmas card with Processing!
Make a family todo list with Sinatra
Let's make a smart home with Ruby!
Make a login function with Rails anyway
[docker] [nginx] Make a simple ALB with nginx
Use FacesContext as a Mock with PowerMockito
[Spring Boot] I stumbled upon a method call count test (Spock framework)
Make a site template easily with Rails
Test controller with Mock MVC in Spring Boot
Let's make a search function with Rails (ransack)
Test code using mock with JUnit (EasyMock center)
Creating a Dockerfile with all AWS CloudFormation test tools
Run Scala with GraalVM & make it a native image
Let's make a LINE Bot with Ruby + Sinatra --Part 2
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
[Java basics] Let's make a triangle with a for statement
[Personal application work memo] Make a calendar with simple_calendar
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Let's make a LINE Bot with Ruby + Sinatra --Part 1
I wrote a test with Spring Boot + JUnit 5 now