TDD: spies, mocks and argumentcaptors
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 5 Nov 2015 18:13:38 +0000 (19:13 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 5 Nov 2015 18:13:38 +0000 (19:13 +0100)
TDD/pom.xml
TDD/src/main/java/de/example/model/mapper/UsersMapper.java [new file with mode: 0644]
TDD/src/main/java/de/example/model/resource/UserResource.java [new file with mode: 0644]
TDD/src/main/java/de/example/services/UsersService.java [new file with mode: 0644]
TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java [new file with mode: 0644]
TDD/src/test/java/de/example/services/UsersServiceTest.java [new file with mode: 0644]

index 9dcea75..1250f6d 100644 (file)
             <version>4.12-beta-1</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+               <groupId>org.mockito</groupId>
+               <artifactId>mockito-core</artifactId>
+               <version>2.0.31-beta</version>
+               <scope>test</scope>
+               </dependency>
     </dependencies>
     <build>
         <plugins>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.1</version>
                 <configuration>
-                                       <!--
                     <source>1.8</source>
                     <target>1.8</target>
-                                       -->
+                                       <!--
                                        <source>1.6</source>
                     <target>1.6</target>
+                    -->
                     <encoding>${project.build.sourceEncoding}</encoding>
                     <compilerArgument>-Xlint:deprecation</compilerArgument>
                 </configuration>
diff --git a/TDD/src/main/java/de/example/model/mapper/UsersMapper.java b/TDD/src/main/java/de/example/model/mapper/UsersMapper.java
new file mode 100644 (file)
index 0000000..aa45c04
--- /dev/null
@@ -0,0 +1,11 @@
+package de.example.model.mapper;
+
+import de.example.model.resource.UserResource;
+
+
+public interface UsersMapper {
+
+       public UserResource findOne(long id);
+
+       public void create(UserResource user);
+}
diff --git a/TDD/src/main/java/de/example/model/resource/UserResource.java b/TDD/src/main/java/de/example/model/resource/UserResource.java
new file mode 100644 (file)
index 0000000..b2e878a
--- /dev/null
@@ -0,0 +1,52 @@
+package de.example.model.resource;
+
+
+public class UserResource {
+       private final Long id;
+       private final String name;
+       
+       public UserResource(Long id, String name) {
+               this.id = id;
+               this.name = name;
+       }
+
+    public Long getId() {
+       return id;
+    }
+
+       
+    public String getName() {
+       return name;
+    }
+
+       @Override
+    public int hashCode() {
+           final int prime = 31;
+           int result = 1;
+           result = prime * result + ((name == null) ? 0 : name.hashCode());
+           return result;
+    }
+
+       @Override
+    public boolean equals(Object obj) {
+           if (this == obj) {
+                   return true;
+           }
+           if (obj == null) {
+                   return false;
+           }
+           if (getClass() != obj.getClass()) {
+                   return false;
+           }
+           UserResource other = (UserResource)obj;
+           if (name == null) {
+                   if (other.name != null) {
+                           return false;
+                   }
+           } else if (!name.equals(other.name)) {
+                   return false;
+           }
+           return true;
+    }
+
+}
diff --git a/TDD/src/main/java/de/example/services/UsersService.java b/TDD/src/main/java/de/example/services/UsersService.java
new file mode 100644 (file)
index 0000000..20d416b
--- /dev/null
@@ -0,0 +1,13 @@
+package de.example.services;
+
+import de.example.model.resource.UserResource;
+
+
+public interface UsersService {
+
+       UserResource findOneById(long id);
+
+       void create(UserResource user);
+       
+       String spiedMethod(UserResource user);
+}
diff --git a/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java b/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java
new file mode 100644 (file)
index 0000000..7743cd2
--- /dev/null
@@ -0,0 +1,33 @@
+package de.example.services.impl;
+
+import de.example.model.mapper.UsersMapper;
+import de.example.model.resource.UserResource;
+import de.example.services.UsersService;
+
+
+public class UsersServiceImpl implements UsersService {
+       private final UsersMapper usersMapper;
+       
+       public UsersServiceImpl(final UsersMapper usersMapper) {
+               this.usersMapper = usersMapper;
+       }
+       
+       @Override
+       public UserResource findOneById(long id) {
+           return this.usersMapper.findOne(id);
+           
+    }
+
+       @Override
+    public void create(final UserResource user) {
+               this.spiedMethod(user);
+               
+               usersMapper.create(user); 
+    }
+       
+       @Override
+    public String spiedMethod(final UserResource user) {
+               return user.getName();
+       }
+
+}
diff --git a/TDD/src/test/java/de/example/services/UsersServiceTest.java b/TDD/src/test/java/de/example/services/UsersServiceTest.java
new file mode 100644 (file)
index 0000000..ad8e697
--- /dev/null
@@ -0,0 +1,75 @@
+package de.example.services;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import de.example.model.mapper.UsersMapper;
+import de.example.model.resource.UserResource;
+import de.example.services.impl.UsersServiceImpl;
+
+
+public class UsersServiceTest {
+       private static final long ID = 10L;
+       
+       private UsersService usersService;
+       private UsersMapper usersMapper;
+
+       @Before
+       public void setUp() throws Exception {
+               usersMapper = mock(UsersMapper.class);
+               // USING spy because we want to spy UsersService.spiedMethod
+               // otherwise we wouldn't need org.mockito.Mockito.spy.
+               usersService = spy(new UsersServiceImpl(usersMapper));
+       }
+
+       @Test
+       public void givenSomeIdWhenFindUserThenReturnUser() {
+               // Arrange
+               UserResource expectedUser = new UserResource(ID, "Gustavo");
+               when(usersMapper.findOne(ID)).thenReturn(expectedUser);
+
+               // Act
+               UserResource user = usersService.findOneById(ID);
+               
+               // Assert
+               assertEquals(expectedUser, user);
+       }
+
+       
+       @Test
+       public void givenNewUserDataWhenCreateNewUserThenCreateUser() {
+               // Arrange
+               UserResource expectedUser = new UserResource(ID, "Gustavo");
+               UserResource newUser = new UserResource(null, "Gustavo");
+               
+               // Act
+               usersService.create(newUser);
+               
+               // Assert
+               verify(usersService).spiedMethod(newUser);
+               verify(usersMapper).create(newUser);
+               assertEquals(expectedUser, newUser);            
+       }
+       
+       @Test
+       public void givenNewUserDataWhenCreateNewUserThenCreateUserWithArgumentCaptor() {
+               // Arrange
+               UserResource newUser = new UserResource(null, "Gustavo");
+               ArgumentCaptor<UserResource> argument = ArgumentCaptor.forClass(UserResource.class);
+               
+               // Act
+               usersService.create(newUser);
+               
+               // Assert
+               verify(usersService).spiedMethod(newUser);
+               verify(usersMapper).create(argument.capture());
+               assertEquals(argument.getValue(), newUser);             
+       }
+}