From: Gustavo Martin Morcuende Date: Thu, 5 Nov 2015 18:13:38 +0000 (+0100) Subject: TDD: spies, mocks and argumentcaptors X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=a6d291612685c070f1e267d3694396c0bb46f7b1;p=JavaForFun TDD: spies, mocks and argumentcaptors --- diff --git a/TDD/pom.xml b/TDD/pom.xml index 9dcea75..1250f6d 100644 --- a/TDD/pom.xml +++ b/TDD/pom.xml @@ -69,6 +69,12 @@ 4.12-beta-1 test + + org.mockito + mockito-core + 2.0.31-beta + test + @@ -77,12 +83,12 @@ maven-compiler-plugin 3.1 - + ${project.build.sourceEncoding} -Xlint:deprecation 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 index 0000000..aa45c04 --- /dev/null +++ b/TDD/src/main/java/de/example/model/mapper/UsersMapper.java @@ -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 index 0000000..b2e878a --- /dev/null +++ b/TDD/src/main/java/de/example/model/resource/UserResource.java @@ -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 index 0000000..20d416b --- /dev/null +++ b/TDD/src/main/java/de/example/services/UsersService.java @@ -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 index 0000000..7743cd2 --- /dev/null +++ b/TDD/src/main/java/de/example/services/impl/UsersServiceImpl.java @@ -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 index 0000000..ad8e697 --- /dev/null +++ b/TDD/src/test/java/de/example/services/UsersServiceTest.java @@ -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 argument = ArgumentCaptor.forClass(UserResource.class); + + // Act + usersService.create(newUser); + + // Assert + verify(usersService).spiedMethod(newUser); + verify(usersMapper).create(argument.capture()); + assertEquals(argument.getValue(), newUser); + } +}