New Post

Rss

Tampilkan postingan dengan label RETURNS_DEEP_STUBS. Tampilkan semua postingan
Tampilkan postingan dengan label RETURNS_DEEP_STUBS. Tampilkan semua postingan
Selasa, 06 Agustus 2013
Spock - return nested spies / mocks

Spock - return nested spies / mocks


Hi! Some time ago I have written an article about Mockito and using RETURNS_DEEP_STUBS when working with JAXB. Quite recently we have faced a similliar issue with deeply nesetd JAXB and the awesome testing framework written in Groovy called Spock. Natively Spock does not support creating deep stubs or spies so we needed to create a workaround for it and this article will show you how to do it.

Project structure

We will be working on the same data structure as in the RETURNS_DEEP_STUBS when working with JAXB article so the project structure will be quite simillar:


As you can see the main difference is such that the tests are present in the /test/groovy/ folder instead of /test/java/ folder.

Extended Spock Specification

In order to use Spock as a testing framework you have to create Groovy test scripts that extend the Spock Specification class. The details of how to use Spock are available here. In this project I have created an abstract class that extends Specification and adds two additional methods for creating nested Test Doubles (I don't remember if I haven't seen a prototype of this approach somewhere on the internet).

ExtendedSpockSpecification.groovy

package com.blogspot.toomuchcoding.spock;

import spock.lang.Specification

/**
* Created with IntelliJ IDEA.
* User: MGrzejszczak
* Date: 14.06.13
* Time: 15:26
*/
abstract class ExtendedSpockSpecification extends Specification {
/**
* The method creates nested structure of spies for all the elements present in the property parameter. Those spies are set on the input object.
*
* @param object - object on which you want to create nested spies
* @param property - field accessors delimited by a dot - JavaBean convention
* @return Spy of the last object from the property path
*/
protected def createNestedSpies(object, String property) {
def lastObject = object
property.tokenize('.').inject object, { obj, prop ->
if (obj[prop] == null) {
def foundProp = obj.metaClass.properties.find { it.name == prop }
obj[prop] = Spy(foundProp.type)
}
lastObject = obj[prop]
}
lastObject
}

/**
* The method creates nested structure of mocks for all the elements present in the property parameter. Those mocks are set on the input object.
*
* @param object - object on which you want to create nested mocks
* @param property - field accessors delimited by a dot - JavaBean convention
* @return Mock of the last object from the property path
*/
protected def createNestedMocks(object, String property) {
def lastObject = object
property.tokenize('.').inject object, { obj, prop ->
def foundProp = obj.metaClass.properties.find { it.name == prop }
def mockedProp = Mock(foundProp.type)
lastObject."${prop}" >> mockedProp
lastObject = mockedProp
}
lastObject
}
}

These two methods work in a very simillar manner.
  • Assuming that the method's argument property  looks as follows: "a.b.c.d" then the methods tokenize the string by "." and iterate over the array - ["a","b","c","d"]. 
  • We then iterate over the properties of the Meta Class to find the one whose name is equal to prop (for example "a"). 
  • If that is the case we then use Spock's Mock/Spy method to create a Test Double of a given class (type). 
  • Finally we have to bind the mocked nested element to its parent. 
    • For the Spy it's easy since we set the value on the parent (lastObject = obj[prop]). 
    • For the mocks however we need to use the overloaded >> operator to record the behavior for our mock - that's why dynamically call the property that is present in the prop variable (lastObject."${prop}" >> mockedProp). 
  • Then we return from the closure the mocked/spied instance and we repeat the process for it

Class to be tested

Let's take a look at the class to be tested:

PlayerServiceImpl.java

package com.blogspot.toomuchcoding.service;

import com.blogspot.toomuchcoding.model.PlayerDetails;

/**
* User: mgrzejszczak
* Date: 08.06.13
* Time: 19:02
*/
public class PlayerServiceImpl implements PlayerService {
@Override
public boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country) {
String countryValue = playerDetails.getClubDetails().getCountry().getCountryCode().getCountryCode().value();
return countryValue.equalsIgnoreCase(country);
}
}

The test class

And now the test class:

PlayerServiceImplWrittenUsingSpockTest.groovy

package com.blogspot.toomuchcoding.service

import com.blogspot.toomuchcoding.model.*
import com.blogspot.toomuchcoding.spock.ExtendedSpockSpecification

/**
* User: mgrzejszczak
* Date: 14.06.13
* Time: 16:06
*/
class PlayerServiceImplWrittenUsingSpockTest extends ExtendedSpockSpecification {

public static final String COUNTRY_CODE_ENG = "ENG";

PlayerServiceImpl objectUnderTest

def setup(){
objectUnderTest = new PlayerServiceImpl()
}

def "should return true if country code is the same when creating nested structures using groovy"() {
given:
PlayerDetails playerDetails = new PlayerDetails(
clubDetails: new ClubDetails(
country: new CountryDetails(
countryCode: new CountryCodeDetails(
countryCode: CountryCodeType.ENG
)
)
)
)

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}

def "should return true if country code is the same when creating nested structures using spock mocks - requires CGLIB for non interface types"() {
given:
PlayerDetails playerDetails = Mock()
ClubDetails clubDetails = Mock()
CountryDetails countryDetails = Mock()
CountryCodeDetails countryCodeDetails = Mock()
countryCodeDetails.countryCode >> CountryCodeType.ENG
countryDetails.countryCode >> countryCodeDetails
clubDetails.country >> countryDetails
playerDetails.clubDetails >> clubDetails

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}


def "should return true if country code is the same using ExtendedSpockSpecification's createNestedMocks"() {
given:
PlayerDetails playerDetails = Mock()
CountryCodeDetails countryCodeDetails = createNestedMocks(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode >> CountryCodeType.ENG

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}

def "should return false if country code is not the same using ExtendedSpockSpecification createNestedMocks"() {
given:
PlayerDetails playerDetails = Mock()
CountryCodeDetails countryCodeDetails = createNestedMocks(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode >> CountryCodeType.PL

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
!playerIsOfGivenCountry
}

def "should return true if country code is the same using ExtendedSpockSpecification's createNestedSpies"() {
given:
PlayerDetails playerDetails = Spy()
CountryCodeDetails countryCodeDetails = createNestedSpies(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode = CountryCodeType.ENG

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}

def "should return false if country code is not the same using ExtendedSpockSpecification's createNestedSpies"() {
given:
PlayerDetails playerDetails = Spy()
CountryCodeDetails countryCodeDetails = createNestedSpies(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode = CountryCodeType.PL

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
!playerIsOfGivenCountry
}


}

Let's move through the test methods one by one. First I present the code and then have a quick description of the presented snippet.

    def "should return true if country code is the same when creating nested structures using groovy"() {
given:
PlayerDetails playerDetails = new PlayerDetails(
clubDetails: new ClubDetails(
country: new CountryDetails(
countryCode: new CountryCodeDetails(
countryCode: CountryCodeType.ENG
)
)
)
)

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}

Here you could find the approach of creating nested structures by using the Groovy feature of passing properties to be set in the constructor.

    def "should return true if country code is the same when creating nested structures using spock mocks - requires CGLIB for non interface types"() {
given:
PlayerDetails playerDetails = Mock()
ClubDetails clubDetails = Mock()
CountryDetails countryDetails = Mock()
CountryCodeDetails countryCodeDetails = Mock()
countryCodeDetails.countryCode >> CountryCodeType.ENG
countryDetails.countryCode >> countryCodeDetails
clubDetails.country >> countryDetails
playerDetails.clubDetails >> clubDetails

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}

Here you can find a test that creates mocks using Spock - mind you that you need CGLIB as a dependency when you are mocking non interface types.

    def "should return true if country code is the same using ExtendedSpockSpecification's createNestedMocks"() {
given:
PlayerDetails playerDetails = Mock()
CountryCodeDetails countryCodeDetails = createNestedMocks(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode >> CountryCodeType.ENG

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}

Here you have an example of creating nested mocks using the createNestedMocks method.

    def "should return false if country code is not the same using ExtendedSpockSpecification createNestedMocks"() {
given:
PlayerDetails playerDetails = Mock()
CountryCodeDetails countryCodeDetails = createNestedMocks(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode >> CountryCodeType.PL

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
!playerIsOfGivenCountry
}

An example showing that creating nested mocks using the createNestedMocks method really works - should return false for improper country code.

    def "should return true if country code is the same using ExtendedSpockSpecification's createNestedSpies"() {
given:
PlayerDetails playerDetails = Spy()
CountryCodeDetails countryCodeDetails = createNestedSpies(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode = CountryCodeType.ENG

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
playerIsOfGivenCountry
}

Here you have an example of creating nested spies using the createNestedSpies method.

    def "should return false if country code is not the same using ExtendedSpockSpecification's createNestedSpies"() {
given:
PlayerDetails playerDetails = Spy()
CountryCodeDetails countryCodeDetails = createNestedSpies(playerDetails, "clubDetails.country.countryCode")
countryCodeDetails.countryCode = CountryCodeType.PL

when:
boolean playerIsOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

then:
!playerIsOfGivenCountry
}

An example showing that creating nested spies using the createNestedSpies method really works - should return false for improper country code.

Summary

In this post I have shown you how you can create nested mocks and spies using Spock. It can be useful especially when you are working with nested structures such as JAXB. Still you have to bear in mind that those structures to some extend violate the Law of Demeter. If you check my previous article about Mockito you would see that:
We are getting the nested elements from the JAXB generated classes. Although it violates the Law of Demeter it is quite common to call methods of structures because JAXB generated classes are in fact structures so in fact I fully agree with Martin Fowler that it should be called the Suggestion of Demeter.
And in case of this example the idea is the same - we are talking about structures so we don't violate the Law of Demeter.

Advantages

  • With a single method you can mock/spy nested elements
  • Code cleaner than creating tons of objects that you then have to manually set

Disadvantages

  • Your IDE won't help you with providing the property names since the properties are presented as Strings
  • You have to set Test Doubles only in the Specification context (and sometimes you want to go outside this scope)

Sources

As usual the sources are available at BitBucket and GitHub.
Sabtu, 08 Juni 2013
Mockito - RETURNS_DEEP_STUBS for JAXB

Mockito - RETURNS_DEEP_STUBS for JAXB


Sorry for not having written for some time but I was busy with writing the JBoss Drools Refcard for DZone and I am in the middle of writing a book about Mockito so I don't have too much time left for blogging...

Anyway quite recently on my current project I had an interesting situation regarding unit testing with Mockito and JAXB structures. We have very deeply nested JAXB structures generated from schemas that are provided for us which means that we can't change it in anyway.



Let's take a look at the project structure:


The project structure is pretty simple - there is a Player.xsd schema file that thanks to using the jaxb2-maven-plugin produces the generated JAXB Java classes corresponding to the schema in the target/jaxb/ folder in the appropriate package that is defined in the pom.xml. Speaking of which let's take a look at the pom.xml file.

The pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.blogspot.toomuchcoding</groupId>
<artifactId>mockito-deep_stubs</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spring-release</id>
<url>http://maven.springframework.org/release</url>
</repository>
<repository>
<id>maven-us-nuxeo</id>
<url>https://maven-us.nuxeo.org/nexus/content/groups/public</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>


<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.blogspot.toomuchcoding.model</packageName>
<schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
</configuration>
</plugin>
</plugins>
</build>

</project>

Apart from the previously defined project dependencies, as mentioned previously in the jaxb2-maven-plugin in the configuration node you can define the packageName value that defines to which package should the JAXB classes be generated basing on the schemaDirectory value where the plugin can find the proper schema files.

Speaking of which let's check the Player.xsd schema file (simillar to the one that was present in the Spring JMS automatic message conversion article of mine):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="PlayerDetails">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Name" type="xsd:string"/>
<xsd:element name="Surname" type="xsd:string"/>
<xsd:element name="Position" type="PositionType"/>
<xsd:element name="Age" type="xsd:int"/>
<xsd:element name="ClubDetails" type="ClubDetails"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="ClubDetails">
<xsd:sequence>
<xsd:element name="TeamName" type="xsd:string"/>
<xsd:element name="Country" type="CountryDetails"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="CountryDetails">
<xsd:sequence>
<xsd:element name="CountryName" type="xsd:string"/>
<xsd:element name="CountryCode" type="CountryCodeDetails"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="CountryCodeDetails">
<xsd:sequence>
<xsd:element name="CountryName" type="xsd:string"/>
<xsd:element name="CountryCode" type="CountryCodeType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:simpleType name="CountryCodeType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="PL"/>
<xsd:enumeration value="GER"/>
<xsd:enumeration value="FRA"/>
<xsd:enumeration value="ENG"/>
<xsd:enumeration value="ESP"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="PositionType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="GK"/>
<xsd:enumeration value="DEF"/>
<xsd:enumeration value="MID"/>
<xsd:enumeration value="ATT"/>
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>

As you can see I'm defining some complex types that even though might have no business sense but you can find such examples in the real life :)

Let's find out how the method that we would like to test looks like. Here we have the PlayerServiceImpl that implements the PlayerService interface:

package com.blogspot.toomuchcoding.service;

import com.blogspot.toomuchcoding.model.PlayerDetails;

/**
* User: mgrzejszczak
* Date: 08.06.13
* Time: 19:02
*/
public class PlayerServiceImpl implements PlayerService {
@Override
public boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country) {
String countryValue = playerDetails.getClubDetails().getCountry().getCountryCode().getCountryCode().value();
return countryValue.equalsIgnoreCase(country);
}
}

We are getting the nested elements from the JAXB generated classes. Although it violates the Law of Demeter it is quite common to call methods of structures because JAXB generated classes are in fact structures so in fact I fully agree with Martin Fowler that it should be called the Suggestion of Demeter. Anyway let's see how you could test the method:

    @Test
public void shouldReturnTrueIfCountryCodeIsTheSame() throws Exception {
//given
PlayerDetails playerDetails = new PlayerDetails();
ClubDetails clubDetails = new ClubDetails();
CountryDetails countryDetails = new CountryDetails();
CountryCodeDetails countryCodeDetails = new CountryCodeDetails();
playerDetails.setClubDetails(clubDetails);
clubDetails.setCountry(countryDetails);
countryDetails.setCountryCode(countryCodeDetails);
countryCodeDetails.setCountryCode(CountryCodeType.ENG);

//when
boolean playerOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

//then
assertThat(playerOfGivenCountry, is(true));
}
The function checks if, once you have the same Country Code, you get a true boolean from the method. The only problem is the amount of sets and instantiations that take place when you want to create the input message. In our projects we have twice as many nested elements so you can only imagine the number of code that we would have to produce to create the input object...

So what can be done to improve this code? Mockito comes to the rescue to together with the RETURN_DEEP_STUBS default answer to the Mockito.mock(...) method:

  @Test
public void shouldReturnTrueIfCountryCodeIsTheSameUsingMockitoReturnDeepStubs() throws Exception {
//given
PlayerDetails playerDetailsMock = mock(PlayerDetails.class, RETURNS_DEEP_STUBS);
CountryCodeType countryCodeType = CountryCodeType.ENG;
when(playerDetailsMock.getClubDetails().getCountry().getCountryCode().getCountryCode()).thenReturn(countryCodeType);

//when
boolean playerOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetailsMock, COUNTRY_CODE_ENG);

//then
assertThat(playerOfGivenCountry, is(true));
}
So what happened here is that you use the Mockito.mock(...) method and provide the RETURNS_DEEP_STUBS answer that will create mocks automatically for you. Mind you that Enums can't be mocked that's why you can't write in the Mockito.when(...) function playerDetailsMock.getClubDetails().getCountry().getCountryCode().getCountryCode().getValue().

Summing it up you can compare the readability of both tests and see how clearer it is to work with JAXB structures by using Mockito RETURNS_DEEP_STUBS default answer.

Naturally sources for this example are available at BitBucket and GitHub.


Copyright © 2012 Codding News All Right Reserved