Spock is a testing and specification framework for Java and Groovy applications. It combines the main advantages of JUnit, BDD and Mocking and is considered a game changer for unit testing. It ensures that all tests written using the framework follow BDD conventions; therefore, the test case is very readable and parameterized.
The only pity is that Spock do not support Mocking static methods, private methods write with java and final classes, final fields also can't be Mocked.
Introducing PowerMock to mock static methods is too cumbersome, it is not prepared for spock, there may be incompatibility
For this, we developed this spock plugin to enhance its capabilities in Spock way
In addition to introducing Spock dependencies, add dependencies on spock-mockfree
<dependency>
<groupId>com.sayweee</groupId>
<artifactId>spock.mockfree</artifactId>
<version>1.0.3</version>
<scope>test</scope>
</dependency>
Now you have the ability to mock static methods and final classes
We used a static method returnA of StaticMethodClass class
public class StaticMethodClass {
public static String returnA() {
return "A";
}
}
here is the calling code
public class CallStaticMethodClass {
public String useStatic() {
return StaticMethodClass.returnA();
}
}
Now we need to test the useStatic method of CallStaticMethodClass class But spock itself does not support mock static methods, and we support
class CallStaticMethodClassTest extends Specification {
def 'call static method is mocked method'() {
given:
CallStaticMethodClass callStaticMethodClass = Spy()
println("useStatic")
expect:
callStaticMethodClass.useStatic() == 'M'
}
@MockStatic(StaticMethodClass)
public static String returnA() {
return "M";
}
}
We use the @MockStatic annotation to mark which class needs to be mocked
Directly implement the static method that requires mocking under it,
the method signature remains the same, but the implementation is different
Run the test, you will find that the StaticMethodClass.returnA() called in the tested class is replaced by the implementation of CallStaticMethodClassTest.returnA
Spock does not support mock Final class and will throw an Exception
Just like Person class
public final class Person {
private String firstName = "Weee";
private String lastName = "Weee";
private static final String country = "";
private final Address address = new Address("Fremont Blvd");
private Person() {
}
protected final String getFirstName() {
return firstName;
}
private String getLastName() {
return lastName;
}
private final Address getAddress() {
return address;
}
private final static class Address {
private final String street;
private Address(final String street) {
this.street = street;
}
public String getStreet() {
return street;
}
}
}
Even we have a private final class Address inside,Let's write a test
there's nothing we can't mock, that's why we call it mockfree
@MockFree([Person, Person.Address])
class PersonTest extends Specification {
def mockUtil = new MockUtil()
def 'final from class is removed'() {
setup:
Person person = Mock()
expect:
mockUtil.isMock person
}
def 'final from subclass is removed'() {
setup:
Person.Address address = Mock()
expect:
mockUtil.isMock address
}
def 'final from method is removed'() {
setup:
Person person = Mock()
when:
def res = person.firstName
then:
1 * person.firstName >> 'Weee!'
expect:
res == 'Weee!'
}
def 'private on method is now public'() {
setup:
Person person = Mock()
when:
def res = person.lastName
then:
1 * person.lastName >> 'Weee!'
expect:
res == 'Weee!'
}
def 'final is removed and private on method is now public'() {
setup:
Person person = Mock()
when:
def res = person.address.street
then:
1 * person.address >> new Person.Address('Fremont Blvd!')
expect:
res == 'Fremont Blvd!'
}
def 'private final on property is removed'() {
setup:
Person person = new Person()
when:
person.address = new Person.Address("street")
person.country = "china"
then:
person.address.street == "street"
person.country == "china"
}
}
We use the @MockFree annotation to annotate the classes that need to remove the Final Private restriction And That is All,make you free to mock everything!
- First Release
- Bug Fix
- Support mock final properties
- Add TestExecutionListener for Transformer install before @MockFree's classes is loaded