BAEL-88 Testing in Spring Boot (#1722)
This commit is contained in:
parent
a7b7ccad2d
commit
cee7e71400
|
@ -1,7 +1,6 @@
|
|||
package org.baeldung.boot.boottest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
|
@ -12,9 +11,9 @@ import org.springframework.stereotype.Repository;
|
|||
@Transactional
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
|
||||
public Optional<Employee> findByName(String name);
|
||||
public Employee findByName(String name);
|
||||
|
||||
public Optional<Employee> findById(Long id);
|
||||
public Employee findById(Long id);
|
||||
|
||||
public List<Employee> findAll();
|
||||
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
package org.baeldung.boot.boottest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeService {
|
||||
|
||||
public Optional<Employee> getEmployeeById(Long id);
|
||||
public Employee getEmployeeById(Long id);
|
||||
|
||||
public Optional<Employee> getEmployeeByName(String name);
|
||||
public Employee getEmployeeByName(String name);
|
||||
|
||||
public List<Employee> getAllEmployees();
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.baeldung.boot.boottest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
|
@ -16,12 +15,12 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Override
|
||||
public Optional<Employee> getEmployeeById(Long id) {
|
||||
public Employee getEmployeeById(Long id) {
|
||||
return employeeRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Employee> getEmployeeByName(String name) {
|
||||
public Employee getEmployeeByName(String name) {
|
||||
return employeeRepository.findByName(name);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,11 +3,7 @@ package org.baeldung.boot.boottest;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.baeldung.boot.boottest.Employee;
|
||||
import org.baeldung.boot.boottest.EmployeeRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -27,18 +23,17 @@ public class EmployeeRepositoryIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void whenFindByName_thenReturnEmployee() {
|
||||
Employee emp = new Employee("test");
|
||||
entityManager.persistAndFlush(emp);
|
||||
Employee alex = new Employee("alex");
|
||||
entityManager.persistAndFlush(alex);
|
||||
|
||||
Optional<Employee> fromDb = employeeRepository.findByName(emp.getName());
|
||||
assertThat(fromDb.get()
|
||||
.getName()).isEqualTo(emp.getName());
|
||||
Employee found = employeeRepository.findByName(alex.getName());
|
||||
assertThat(found.getName()).isEqualTo(alex.getName());
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void whenInvalidName_thenNoSuchElementException() {
|
||||
Optional<Employee> fromDb = employeeRepository.findByName("doesNotExist");
|
||||
fromDb.get();
|
||||
@Test
|
||||
public void whenInvalidName_thenReturnNull() {
|
||||
Employee fromDb = employeeRepository.findByName("doesNotExist");
|
||||
assertThat(fromDb).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -46,15 +41,14 @@ public class EmployeeRepositoryIntegrationTest {
|
|||
Employee emp = new Employee("test");
|
||||
entityManager.persistAndFlush(emp);
|
||||
|
||||
Optional<Employee> fromDb = employeeRepository.findById(emp.getId());
|
||||
assertThat(fromDb.get()
|
||||
.getName()).isEqualTo(emp.getName());
|
||||
Employee fromDb = employeeRepository.findById(emp.getId());
|
||||
assertThat(fromDb.getName()).isEqualTo(emp.getName());
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
public void whenInvalidId_thenNoSuchElementException() {
|
||||
Optional<Employee> fromDb = employeeRepository.findById(-11L);
|
||||
fromDb.get();
|
||||
@Test
|
||||
public void whenInvalidId_thenReturnNull() {
|
||||
Employee fromDb = employeeRepository.findById(-11L);
|
||||
assertThat(fromDb).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -74,5 +68,4 @@ public class EmployeeRepositoryIntegrationTest {
|
|||
.extracting(Employee::getName)
|
||||
.containsOnly(alex.getName(), ron.getName(), bob.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,13 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.baeldung.boot.boottest.Employee;
|
||||
import org.baeldung.boot.boottest.EmployeeRepository;
|
||||
import org.baeldung.boot.boottest.EmployeeService;
|
||||
import org.baeldung.boot.boottest.EmployeeServiceImpl;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -44,38 +38,38 @@ public class EmployeeServiceImplIntegrationTest {
|
|||
Employee john = new Employee("john");
|
||||
john.setId(11L);
|
||||
|
||||
Optional<Employee> emp = Optional.of(john);
|
||||
|
||||
Employee bob = new Employee("bob");
|
||||
Employee alex = new Employee("alex");
|
||||
|
||||
List<Employee> allEmployees = Arrays.asList(john, bob, alex);
|
||||
|
||||
Mockito.when(employeeRepository.findByName(john.getName()))
|
||||
.thenReturn(emp);
|
||||
.thenReturn(john);
|
||||
Mockito.when(employeeRepository.findByName(alex.getName()))
|
||||
.thenReturn(alex);
|
||||
Mockito.when(employeeRepository.findByName("wrong_name"))
|
||||
.thenReturn(Optional.empty());
|
||||
.thenReturn(null);
|
||||
Mockito.when(employeeRepository.findById(john.getId()))
|
||||
.thenReturn(emp);
|
||||
.thenReturn(john);
|
||||
Mockito.when(employeeRepository.findAll())
|
||||
.thenReturn(allEmployees);
|
||||
Mockito.when(employeeRepository.findById(-99L))
|
||||
.thenReturn(Optional.empty());
|
||||
.thenReturn(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidName_thenEmployeeShouldBeFound() {
|
||||
Optional<Employee> fromDb = employeeService.getEmployeeByName("john");
|
||||
assertThat(fromDb.get()
|
||||
.getName()).isEqualTo("john");
|
||||
String name = "alex";
|
||||
Employee found = employeeService.getEmployeeByName(name);
|
||||
|
||||
assertThat(found.getName())
|
||||
.isEqualTo(name);
|
||||
}
|
||||
|
||||
verifyFindByNameIsCalledOnce("john");
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
@Test
|
||||
public void whenInValidName_thenEmployeeShouldNotBeFound() {
|
||||
Optional<Employee> fromDb = employeeService.getEmployeeByName("wrong_name");
|
||||
fromDb.get();
|
||||
Employee fromDb = employeeService.getEmployeeByName("wrong_name");
|
||||
assertThat(fromDb).isNull();
|
||||
|
||||
verifyFindByNameIsCalledOnce("wrong_name");
|
||||
}
|
||||
|
@ -97,19 +91,18 @@ public class EmployeeServiceImplIntegrationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenValidI_thendEmployeeShouldBeFound() {
|
||||
Optional<Employee> fromDb = employeeService.getEmployeeById(11L);
|
||||
assertThat(fromDb.get()
|
||||
.getName()).isEqualTo("john");
|
||||
public void whenValidId_thenEmployeeShouldBeFound() {
|
||||
Employee fromDb = employeeService.getEmployeeById(11L);
|
||||
assertThat(fromDb.getName()).isEqualTo("john");
|
||||
|
||||
verifyFindByIdIsCalledOnce();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchElementException.class)
|
||||
@Test
|
||||
public void whenInValidId_thenEmployeeShouldNotBeFound() {
|
||||
Optional<Employee> fromDb = employeeService.getEmployeeById(-99L);
|
||||
Employee fromDb = employeeService.getEmployeeById(-99L);
|
||||
verifyFindByIdIsCalledOnce();
|
||||
fromDb.get();
|
||||
assertThat(fromDb).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue