Commits

Marcin Grzejszczak  committed cab9f99

[Mockito - With Interfaces] Initial commit

  • Participants
  • Parent commits fc1bc01

Comments (0)

Files changed (17)

File Unit Testing/Mockito - With Interfaces/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-interfaces</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 - With Interfaces/src/main/java/com/blogspot/toomuchcoding/adapter/CommonPlayerAdapter.java

+package com.blogspot.toomuchcoding.adapter;
+
+import com.blogspot.toomuchcoding.model.Player;
+import com.blogspot.toomuchcoding.model.PlayerDetails;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:42
+ */
+public class CommonPlayerAdapter implements Player {
+    private final PlayerDetails playerDetails;
+
+    public CommonPlayerAdapter(PlayerDetails playerDetails){
+        this.playerDetails = playerDetails;
+    }
+
+    @Override
+    public void run() {
+        System.out.printf("Run %s. Run!%n", playerDetails.getName());
+    }
+
+    public PlayerDetails getPlayerDetails() {
+        return playerDetails;
+    }
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/adapter/DefencePlayerAdapter.java

+package com.blogspot.toomuchcoding.adapter;
+
+import com.blogspot.toomuchcoding.model.DJ;
+import com.blogspot.toomuchcoding.model.DefensivePlayer;
+import com.blogspot.toomuchcoding.model.JavaDeveloper;
+import com.blogspot.toomuchcoding.model.PlayerDetails;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:42
+ */
+public class DefencePlayerAdapter extends CommonPlayerAdapter implements DefensivePlayer, DJ, JavaDeveloper {
+
+    public DefencePlayerAdapter(PlayerDetails playerDetails){
+        super(playerDetails);
+    }
+
+    @Override
+    public void defend(){
+        System.out.printf("Defence! %s. Defence!%n", getPlayerDetails().getName());
+    }
+
+    @Override
+    public void playSomeMusic() {
+        System.out.println("Oops I did it again...!");
+    }
+
+    @Override
+    public void doSomeSeriousCoding() {
+        System.out.println("System.out.println(\"Hello world\");");
+    }
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/adapter/OffensivePlayerAdapter.java

+package com.blogspot.toomuchcoding.adapter;
+
+import com.blogspot.toomuchcoding.model.OffensivePlayer;
+import com.blogspot.toomuchcoding.model.PlayerDetails;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:42
+ */
+public class OffensivePlayerAdapter extends CommonPlayerAdapter implements OffensivePlayer {
+
+    public OffensivePlayerAdapter(PlayerDetails playerDetails){
+        super(playerDetails);
+    }
+
+    @Override
+    public void shoot(){
+        System.out.printf("%s Shooooot!.%n", getPlayerDetails().getName());
+    }
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/factory/PlayerFactory.java

+package com.blogspot.toomuchcoding.factory;
+
+import com.blogspot.toomuchcoding.model.Player;
+import com.blogspot.toomuchcoding.model.PositionType;
+
+/**
+ * User: mgrzejszczak
+ * Date: 28.05.13
+ * Time: 23:16
+ */
+public interface PlayerFactory {
+    Player createPlayer(PositionType positionType);
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/factory/PlayerFactoryImpl.java

+package com.blogspot.toomuchcoding.factory;
+
+import com.blogspot.toomuchcoding.adapter.CommonPlayerAdapter;
+import com.blogspot.toomuchcoding.adapter.DefencePlayerAdapter;
+import com.blogspot.toomuchcoding.adapter.OffensivePlayerAdapter;
+import com.blogspot.toomuchcoding.model.Player;
+import com.blogspot.toomuchcoding.model.PlayerDetails;
+import com.blogspot.toomuchcoding.model.PositionType;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:53
+ */
+
+public class PlayerFactoryImpl implements PlayerFactory {
+
+    @Override
+    public Player createPlayer(PositionType positionType) {
+        PlayerDetails player = createCommonPlayer(positionType);
+        switch (positionType){
+            case ATT:
+                return new OffensivePlayerAdapter(player);
+            case MID:
+                return new OffensivePlayerAdapter(player);
+            case DEF:
+                return new DefencePlayerAdapter(player);
+            case GK:
+                return new DefencePlayerAdapter(player);
+            default:
+                return new CommonPlayerAdapter(player);
+        }
+    }
+
+    private PlayerDetails createCommonPlayer(PositionType positionType){
+        PlayerDetails playerDetails = new PlayerDetails();
+        playerDetails.setPosition(positionType);
+        return playerDetails;
+    }
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/factory/PlayerFactoryImplWithFieldSettingLogic.java

+package com.blogspot.toomuchcoding.factory;
+
+import com.blogspot.toomuchcoding.adapter.CommonPlayerAdapter;
+import com.blogspot.toomuchcoding.adapter.DefencePlayerAdapter;
+import com.blogspot.toomuchcoding.adapter.OffensivePlayerAdapter;
+import com.blogspot.toomuchcoding.model.*;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:53
+ */
+
+public class PlayerFactoryImplWithFieldSettingLogic implements PlayerFactory {
+
+    @Override
+    public Player createPlayer(PositionType positionType) {
+        PlayerDetails player = createCommonPlayer(positionType);
+        switch (positionType){
+            case ATT:
+                return createOffensivePlayer(player);
+            case MID:
+                return createOffensivePlayer(player);
+            case DEF:
+                return createDefensivePlayer(player);
+            case GK:
+                return createDefensivePlayer(player);
+            default:
+                return new CommonPlayerAdapter(player);
+        }
+    }
+
+    private Player createDefensivePlayer(PlayerDetails player) {
+        DefencePlayerAdapter defencePlayerAdapter = new DefencePlayerAdapter(player);
+        defencePlayerAdapter.defend();
+        defencePlayerAdapter.playSomeMusic();
+        defencePlayerAdapter.doSomeSeriousCoding();
+        return defencePlayerAdapter;
+    }
+
+    private OffensivePlayer createOffensivePlayer(PlayerDetails player) {
+        OffensivePlayer offensivePlayer = new OffensivePlayerAdapter(player);
+        offensivePlayer.shoot();
+        return offensivePlayer;
+    }
+
+    private PlayerDetails createCommonPlayer(PositionType positionType){
+        PlayerDetails playerDetails = new PlayerDetails();
+        playerDetails.setPosition(positionType);
+        return playerDetails;
+    }
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/DJ.java

+package com.blogspot.toomuchcoding.model;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 22:19
+ */
+public interface DJ {
+    void playSomeMusic();
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/DefensivePlayer.java

+package com.blogspot.toomuchcoding.model;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:44
+ */
+public interface DefensivePlayer extends Player {
+    void defend();
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/JavaDeveloper.java

+package com.blogspot.toomuchcoding.model;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 22:20
+ */
+public interface JavaDeveloper {
+    void doSomeSeriousCoding();
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/OffensivePlayer.java

+package com.blogspot.toomuchcoding.model;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:44
+ */
+public interface OffensivePlayer extends Player {
+    void shoot();
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/model/Player.java

+package com.blogspot.toomuchcoding.model;
+
+/**
+ * User: mgrzejszczak
+ * Date: 09.06.13
+ * Time: 15:44
+ */
+public interface Player {
+    void run();
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/service/PlayerService.java

+package com.blogspot.toomuchcoding.service;
+
+import com.blogspot.toomuchcoding.model.Player;
+import com.blogspot.toomuchcoding.model.PositionType;
+
+/**
+ * User: mgrzejszczak
+ * Date: 08.06.13
+ * Time: 18:59
+ */
+public interface PlayerService {
+    Player playAGameWithAPlayerOfPosition(PositionType positionType);
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/service/PlayerServiceImpl.java

+package com.blogspot.toomuchcoding.service;
+
+import com.blogspot.toomuchcoding.factory.PlayerFactory;
+import com.blogspot.toomuchcoding.model.*;
+
+/**
+ * User: mgrzejszczak
+ * Date: 08.06.13
+ * Time: 19:02
+ */
+public class PlayerServiceImpl implements PlayerService {
+
+    private PlayerFactory playerFactory;
+
+    @Override
+    public Player playAGameWithAPlayerOfPosition(PositionType positionType) {
+        Player player = playerFactory.createPlayer(positionType);
+        player.run();
+        performAdditionalActions(player);
+        return player;
+    }
+
+    private void performAdditionalActions(Player player) {
+        if(player instanceof OffensivePlayer){
+            OffensivePlayer offensivePlayer = (OffensivePlayer) player;
+            performAdditionalActionsForTheOffensivePlayer(offensivePlayer);
+        }else if(player instanceof DefensivePlayer){
+            DefensivePlayer defensivePlayer = (DefensivePlayer) player;
+            performAdditionalActionsForTheDefensivePlayer(defensivePlayer);
+        }
+    }
+
+    private void performAdditionalActionsForTheOffensivePlayer(OffensivePlayer offensivePlayer){
+        offensivePlayer.shoot();
+    }
+
+    private void performAdditionalActionsForTheDefensivePlayer(DefensivePlayer defensivePlayer){
+        defensivePlayer.defend();
+        try{
+            DJ dj = (DJ)defensivePlayer;
+            dj.playSomeMusic();
+            JavaDeveloper javaDeveloper = (JavaDeveloper)defensivePlayer;
+            javaDeveloper.doSomeSeriousCoding();
+        }catch(ClassCastException exception){
+            System.err.println("Sorry, I can't do more than just play football...");
+        }
+    }
+
+    public PlayerFactory getPlayerFactory() {
+        return playerFactory;
+    }
+
+    public void setPlayerFactory(PlayerFactory playerFactory) {
+        this.playerFactory = playerFactory;
+    }
+}

File Unit Testing/Mockito - With Interfaces/src/main/java/com/blogspot/toomuchcoding/service/PlayerServiceImplWithoutUnnecessaryLogic.java

+package com.blogspot.toomuchcoding.service;
+
+import com.blogspot.toomuchcoding.factory.PlayerFactory;
+import com.blogspot.toomuchcoding.model.*;
+
+/**
+ * User: mgrzejszczak
+ * Date: 08.06.13
+ * Time: 19:02
+ */
+public class PlayerServiceImplWithoutUnnecessaryLogic implements PlayerService {
+
+    private PlayerFactory playerFactory;
+
+    /**
+     * What's the point in having this method then?
+     * @param positionType
+     * @return
+     */
+    @Override
+    public Player playAGameWithAPlayerOfPosition(PositionType positionType) {
+        return playerFactory.createPlayer(positionType);
+    }
+
+    public PlayerFactory getPlayerFactory() {
+        return playerFactory;
+    }
+
+    public void setPlayerFactory(PlayerFactory playerFactory) {
+        this.playerFactory = playerFactory;
+    }
+}

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

+package com.blogspot.toomuchcoding.service;
+
+import com.blogspot.toomuchcoding.factory.PlayerFactory;
+import com.blogspot.toomuchcoding.model.*;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.BDDMockito.*;
+
+/**
+ * User: mgrzejszczak
+ * Date: 08.06.13
+ * Time: 19:26
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class PlayerServiceImplTest {
+
+    @Mock
+    PlayerFactory playerFactory;
+
+    @InjectMocks
+    PlayerServiceImpl objectUnderTest;
+
+    @Mock(extraInterfaces = {DJ.class, JavaDeveloper.class})
+    DefensivePlayer defensivePlayerWithDjAndJavaDevSkills;
+
+    @Mock
+    DefensivePlayer defensivePlayer;
+
+    @Mock
+    OffensivePlayer offensivePlayer;
+
+    @Mock
+    Player commonPlayer;
+
+    @Test
+    public void shouldReturnOffensivePlayerThatRan() throws Exception {
+        //given
+        given(playerFactory.createPlayer(PositionType.ATT)).willReturn(offensivePlayer);
+
+        //when
+        Player createdPlayer = objectUnderTest.playAGameWithAPlayerOfPosition(PositionType.ATT);
+
+        //then
+        assertThat(createdPlayer == offensivePlayer, is(true));
+        verify(offensivePlayer).run();
+    }
+
+    @Test
+    public void shouldReturnDefensivePlayerButHeWontBeADjNorAJavaDev() throws Exception {
+        //given
+        given(playerFactory.createPlayer(PositionType.GK)).willReturn(defensivePlayer);
+
+        //when
+        Player createdPlayer = objectUnderTest.playAGameWithAPlayerOfPosition(PositionType.GK);
+
+        //then
+        assertThat(createdPlayer == defensivePlayer, is(true));
+        verify(defensivePlayer).run();
+        verify(defensivePlayer).defend();
+        verifyNoMoreInteractions(defensivePlayer);
+    }
+
+    @Test
+    public void shouldReturnDefensivePlayerBeingADjAndAJavaDev() throws Exception {
+        //given
+        given(playerFactory.createPlayer(PositionType.GK)).willReturn(defensivePlayerWithDjAndJavaDevSkills);
+        doAnswer(new Answer<Object>() {
+            @Override
+            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
+                System.out.println("Hit me baby one more time!");
+                return null;
+            }
+        }).when(((DJ) defensivePlayerWithDjAndJavaDevSkills)).playSomeMusic();
+        doAnswer(new Answer<Object>() {
+            @Override
+            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
+                System.out.println("public static void main(String... args){\n}");
+                return null;
+            }
+        }).when(((JavaDeveloper) defensivePlayerWithDjAndJavaDevSkills)).doSomeSeriousCoding();
+
+        //when
+        Player createdPlayer = objectUnderTest.playAGameWithAPlayerOfPosition(PositionType.GK);
+
+        //then
+        assertThat(createdPlayer == defensivePlayerWithDjAndJavaDevSkills, is(true));
+        verify(defensivePlayerWithDjAndJavaDevSkills).run();
+        verify(defensivePlayerWithDjAndJavaDevSkills).defend();
+        verify((DJ) defensivePlayerWithDjAndJavaDevSkills).playSomeMusic();
+        verify((JavaDeveloper) defensivePlayerWithDjAndJavaDevSkills).doSomeSeriousCoding();
+    }
+
+    @Test
+    public void shouldReturnDefensivePlayerBeingADjAndAJavaDevByUsingWithSettings() throws Exception {
+        //given
+        DefensivePlayer defensivePlayerWithDjAndJavaDevSkills = mock(DefensivePlayer.class, withSettings().extraInterfaces(DJ.class, JavaDeveloper.class));
+        given(playerFactory.createPlayer(PositionType.GK)).willReturn(defensivePlayerWithDjAndJavaDevSkills);
+        doAnswer(new Answer<Object>() {
+            @Override
+            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
+                System.out.println("Hit me baby one more time!");
+                return null;
+            }
+        }).when(((DJ) defensivePlayerWithDjAndJavaDevSkills)).playSomeMusic();
+        doAnswer(new Answer<Object>() {
+            @Override
+            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
+                System.out.println("public static void main(String... args){\n}");
+                return null;
+            }
+        }).when(((JavaDeveloper) defensivePlayerWithDjAndJavaDevSkills)).doSomeSeriousCoding();
+
+        //when
+        Player createdPlayer = objectUnderTest.playAGameWithAPlayerOfPosition(PositionType.GK);
+
+        //then
+        assertThat(createdPlayer == defensivePlayerWithDjAndJavaDevSkills, is(true));
+        verify(defensivePlayerWithDjAndJavaDevSkills).run();
+        verify(defensivePlayerWithDjAndJavaDevSkills).defend();
+        verify((DJ) defensivePlayerWithDjAndJavaDevSkills).playSomeMusic();
+        verify((JavaDeveloper) defensivePlayerWithDjAndJavaDevSkills).doSomeSeriousCoding();
+    }
+
+    @Test
+    public void shouldReturnCommonPlayer() throws Exception {
+        //given
+        given(playerFactory.createPlayer(null)).willReturn(commonPlayer);
+
+        //when
+        Player createdPlayer = objectUnderTest.playAGameWithAPlayerOfPosition(null);
+
+        //then
+        assertThat(createdPlayer, is(commonPlayer));
+    }
+}