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)
Java8 Groovy 2.5 spock-core 1.2-groovy-2.5 Gradle4.10
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.
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")
}
}
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