Commits

Marcin Grzejszczak  committed f11277b

[Spock - Deep stubs] Initial commit

  • Participants
  • Parent commits cab9f99

Comments (0)

Files changed (6)

File Unit Testing/Spock - Deep Stubs/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>spock-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>
+        <spock.version>0.7-groovy-2.0</spock.version>
+	</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>
+        <!-- Spock -->
+        <dependency>
+            <groupId>org.spockframework</groupId>
+            <artifactId>spock-core</artifactId>
+            <version>${spock.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>cglib</groupId>
+            <artifactId>cglib-nodep</artifactId>
+            <version>2.2.2</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>
+			<!-- Spock -->
+            <plugin>
+                <groupId>org.codehaus.gmaven</groupId>
+                <artifactId>gmaven-plugin</artifactId>
+                <version>1.4</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>testCompile</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <dependencies>
+                    <dependency>
+                        <groupId>org.codehaus.groovy.maven.runtime</groupId>
+                        <artifactId>gmaven-runtime-api</artifactId>
+                        <version>1.0</version>
+                        <exclusions>
+                            <exclusion>
+                                <groupId>org.codehaus.groovy</groupId>
+                                <artifactId>groovy-all-minimal</artifactId>
+                            </exclusion>
+                        </exclusions>
+                    </dependency>
+                    <dependency>
+                        <groupId>org.codehaus.groovy</groupId>
+                        <artifactId>groovy-all</artifactId>
+                        <version>2.0.5</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

File Unit Testing/Spock - Deep Stubs/src/main/java/com/blogspot/toomuchcoding/service/PlayerService.java

+package com.blogspot.toomuchcoding.service;
+
+import com.blogspot.toomuchcoding.model.PlayerDetails;
+
+/**
+ * User: mgrzejszczak
+ * Date: 08.06.13
+ * Time: 18:59
+ */
+public interface PlayerService {
+    boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country);
+}

File Unit Testing/Spock - Deep Stubs/src/main/java/com/blogspot/toomuchcoding/service/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);
+    }
+}

File Unit Testing/Spock - Deep Stubs/src/main/resources/xsd/Player.xsd

+<?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>

File Unit Testing/Spock - Deep Stubs/src/test/groovy/com/blogspot/toomuchcoding/service/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"() {
+        given:
+            //PlayerDetails playerDetails = new PlayerDetails(clubDetails: new ClubDetails(country: new CountryDetails(countryCode: new CountryCodeDetails(countryCode: CountryCodeType.ENG))))
+            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 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
+    }
+
+
+}

File Unit Testing/Spock - Deep Stubs/src/test/groovy/com/blogspot/toomuchcoding/spock/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
+    }
+}