Commit 427a63eb authored by Martin Parák's avatar Martin Parák

Added JPA Tests for Owner Service. Closes #50.

parent 636c56ff
package sk.jusska.sfgpetclinic.service.springdatajpa;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sk.jusska.sfgpetclinic.model.Owner;
import sk.jusska.sfgpetclinic.repositories.OwnerRepository;
import sk.jusska.sfgpetclinic.repositories.PetRepository;
import sk.jusska.sfgpetclinic.repositories.PetTypeRepository;
@ExtendWith(MockitoExtension.class)
class OwnerSDJpaServiceTest {
public static final String LAST_NAME = "Smith";
@Mock
private OwnerRepository ownerRepository;
@Mock
private PetRepository petRepository;
@Mock
private PetTypeRepository petTypeRepository;
@InjectMocks
OwnerSDJpaService service;
Owner returnOwner;
@BeforeEach
void setUp() {
returnOwner = Owner.builder().id(1L).lastName(LAST_NAME).build();
}
@Test
void findByLastName() {
when(ownerRepository.findByLastName(any())).thenReturn(returnOwner);
Owner smith = service.findByLastName(LAST_NAME);
assertEquals(LAST_NAME, smith.getLastName());
verify(ownerRepository).findByLastName(any());
}
@Test
void findAll() {
Set<Owner> returnOwnersSet = new HashSet<>();
returnOwnersSet.add(Owner.builder().id(1L).build());
returnOwnersSet.add(Owner.builder().id(2L).build());
when(ownerRepository.findAll()).thenReturn(returnOwnersSet);
Set<Owner> owners = service.findAll();
assertNotNull(owners);
assertEquals(2, owners.size());
}
@Test
void findById() {
when(ownerRepository.findById(anyLong())).thenReturn(Optional.of(returnOwner));
Owner owner = service.findById(1L);
assertNotNull(owner);
}
@Test
void findByIdNotFound() {
when(ownerRepository.findById(anyLong())).thenReturn(Optional.empty());
Owner owner = service.findById(1L);
assertNull(owner);
}
@Test
void save() {
Owner ownerToSave = Owner.builder().id(1L).build();
when(ownerRepository.save(any())).thenReturn(returnOwner);
Owner savedOwner = service.save(ownerToSave);
assertNotNull(savedOwner);
verify(ownerRepository).save(any());
}
@Test
void delete() {
service.delete(returnOwner);
verify(ownerRepository).delete(any());
}
@Test
void deleteById() {
service.deleteById(1L);
verify(ownerRepository).deleteById(anyLong());
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment