Add a test using mocks

This commit is contained in:
Manuel Thalmann 2023-10-10 17:22:34 +02:00
parent e71fb18fc8
commit f1710e481b

View file

@ -13,7 +13,6 @@ import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.AdditionalAnswers;
@ -102,10 +101,27 @@ public class PersonServiceTest {
}
@Ignore("Test is not implemented")
@Test
public void shouldCreateNewClubMember() {
// TODO tutorial-3.4: Implement a test using Mock
PersonTo nonExistingClubMember = PersonTo.builder()
.memberId(2L)
.build();
User testUser = new User(2L, PERSON_FIRST_NAME, PERSON_LAST_NAME, PERSON_ROLES);
when(personRepository.findByMemberId(2L)).thenReturn(Optional.empty());
when(clubDatabaseDao.getUsers()).thenReturn(Arrays.asList(testUser));
when(personRepository.save(any(Person.class))).thenAnswer(AdditionalAnswers.returnsFirstArg());
// call tested method
Person createdPerson = testSubject.getExistingOrCreatePerson(nonExistingClubMember);
// verify results
verify(personRepository, times(1)).save(any(Person.class));
assertSame(Person.Type.CLUB_MEMBER, createdPerson.getPersonType());
assertSame(2l, createdPerson.getMemberId());
assertSame(PERSON_FIRST_NAME, createdPerson.getFirstName());
assertSame(PERSON_LAST_NAME, createdPerson.getLastName());
}
}