Skip to content

Commit

Permalink
Add :spock-specs:mock-integration subproject (spockframework#902)
Browse files Browse the repository at this point in the history
The new project contains integration tests for using Spock to mock Java
interfaces/classes with cglib/byte-buddy and objenesis, and without any
of them.

The old testCglib task that reran all tests with byte-buddy
deactivated was removed except for spock-spring which contains a few
tests that only run when cglib is being used.
  • Loading branch information
marcphilipp authored and leonard84 committed Sep 11, 2018
1 parent a463f1b commit ab43f8c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 21 deletions.
14 changes: 0 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,6 @@ subprojects {
archives sourcesJar, javadocJar
}

task testCglib(type: Test) {
systemProperty("org.spockframework.mock.ignoreByteBuddy", "true")
testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath
}

if (gradle.startParameter.taskNames.contains("travisCiBuild")
|| gradle.startParameter.taskNames.contains("shippableCiBuild")
|| gradle.startParameter.taskNames.contains("appveyorCiBuild")) {
check.dependsOn testCglib
}

testCglib.mustRunAfter test

tasks.withType(Test) {
def taskName = name
reports {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include "spock-bom"
include "spock-core"
include "spock-specs"
include "spock-specs:mock-integration"
include "spock-spring"
include "spock-spring:spring2-test"
include "spock-spring:spring3-test"
Expand Down
7 changes: 0 additions & 7 deletions spock-report/report.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ test {
exclude "org/spockframework/report/sample/FightOrFlightSpec.class"
}

testCglib {
exclude "org/spockframework/report/sample/ParameterizedFightSpec.class"
exclude "org/spockframework/report/sample/FightOrFlightStory.class"
exclude "org/spockframework/report/sample/FightOrFlightSpec.class"
}

ext.spockLogFileDir = file("$buildDir/spock/logFiles")
ext.spockLogFileName = "spock-log"

Expand Down Expand Up @@ -75,4 +69,3 @@ task sampleReport(type: org.spockframework.gradle.GenerateSpockReport) {
spockReportClasspath = sourceSets.main.runtimeClasspath
outputDirectory = file("$buildDir/spock/reports/")
}

39 changes: 39 additions & 0 deletions spock-specs/mock-integration/mock-integration.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
ext.displayName = "Spock Framework - Integration Specs for Mocking"

description = "Integration Specs for Mocking"

configurations {
cglib
bytebuddy
objenesis
}

dependencies {
testCompile(project(":spock-core")) {
exclude group: "cglib"
exclude group: "net.bytebuddy"
exclude group: "org.objenesis"
}
cglib libs.cglib
bytebuddy libs.bytebuddy
objenesis libs.objenesis
}

def codeGenerationLibraries = [
cglib: configurations.cglib,
ByteBuddy: configurations.bytebuddy
]

codeGenerationLibraries.each { key, config ->
tasks.create("test${key.capitalize()}WithoutObjenesis", Test) {
systemProperty("org.spockframework.mock.testType", "${key.toLowerCase()} - objenesis")
classpath += config
}
tasks.create("test${key.capitalize()}WithObjenesis", Test) {
systemProperty("org.spockframework.mock.testType", "${key.toLowerCase()} + objenesis")
classpath += config
classpath += configurations.objenesis
}
}

check.dependsOn(tasks.withType(Test))
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import org.spockframework.mock.CannotCreateMockException
import spock.lang.IgnoreIf
import spock.lang.Requires
import spock.lang.Specification

class MockingIntegrationSpec extends Specification {

static final String TEST_TYPE = System.getProperty("org.spockframework.mock.testType", "plain");

def "can mock interface"() {
given:
def list = Mock(List)

when:
list.add(1)

then:
1 * list.add(1)
}

@IgnoreIf({ TEST_TYPE == "plain" })
def "can mock class when cglib or byte-buddy are present"() {
given:
def list = Mock(ArrayList)

when:
list.add(1)

then:
1 * list.add(1)
}

@Requires({ TEST_TYPE == "plain" })
def "cannot mock class without cglib and byte-buddy"() {
when:
Mock(ArrayList)

then:
thrown(CannotCreateMockException)
}

@IgnoreIf({ TEST_TYPE == "plain" })
def "can spy on class when cglib or byte-buddy are present"() {
given:
def list = Spy(ArrayList)

when:
list.add(1)

then:
1 * list.add(1)
list.get(0) == 1
}

@Requires({ TEST_TYPE == "plain" })
def "cannot spy on class without cglib and byte-buddy"() {
when:
Spy(ArrayList)

then:
thrown(CannotCreateMockException)
}

}
7 changes: 7 additions & 0 deletions spock-spring/spring.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ dependencies {
testRuntime libs.log4j
}

task testCglib(type: Test) {
systemProperty("org.spockframework.mock.ignoreByteBuddy", "true")
mustRunAfter test
}

check.dependsOn testCglib

jar {
manifest {
attributes(
Expand Down

0 comments on commit ab43f8c

Please sign in to comment.