spock-mockable
allows creation of mocks otherwise un-mockable by the Spock Framework.
Spock is not capable of mocking private
or final
classes or methods
because they are not accessible via inheritance. spock-mockable
uses JVM instrumentation to
modify these classes upon class loading and apply looser access restrictions.
In consequence Spock then capable of mocking these classes.
-
Changes methode visibility
private
toprotected
-
Removes
final
from classes and methods -
Automatically attaches java agent
-
Redefines classes via Byte Buddy transformation.
-
Working with Spock Framework 1.3 and 2.0
<depenencies>
<dependency>
<groupId>io.github.joke</groupId>
<artifactId>spock-mockable</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
</depenencies>
Add the @Mockable
annotation to your spock specification.
@Mockable(Person)
class PersonSpec extends Specification {
}
io.github.joke
mockable@Mockable(packages = "io.github.joke")
class PersonSpec extends Specification {
}
More examples can be found in examples.
All @Mockable
annotations are scanned during groovy’s compilation phase for classes and registered.
At the start of a test JVM these classes are transformed by the JVM instrumentation
regardless of the actual test class the annotation has been placed on. This might lead to unexpected behaviour.