Commits

Marcin Grzejszczak committed fc1bc01

[Unit Testing - Mockto Deep Stubs] Initial commit

  • Participants
  • Parent commits e54c971

Comments (0)

Files changed (5)

File Unit Testing/Mockito - 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>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>

File Unit Testing/Mockito - 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/Mockito - 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/Mockito - 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/Mockito - Deep Stubs/src/test/java/com/blogspot/toomuchcoding/service/PlayerServiceImplTest.java

+package com.blogspot.toomuchcoding.service;
+
+import com.blogspot.toomuchcoding.model.*;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * User: mgrzejszczak
+ * Date: 08.06.13
+ * Time: 19:26
+ */
+public class PlayerServiceImplTest {
+
+    public static final String COUNTRY_CODE_ENG = "ENG";
+    PlayerServiceImpl objectUnderTest = new PlayerServiceImpl();
+
+    @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));
+    }
+
+    @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));
+    }
+}