<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>
--- /dev/null
+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;
+ }
+
+}
--- /dev/null
+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();
+ }
+
+}
--- /dev/null
+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);
+ }
+}