JAVA-84: Moved 1 article to spring-boot-testing
This commit is contained in:
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.internal.verification.VerificationModeFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRestController;
|
||||
import com.baeldung.boot.testing.EmployeeService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(value = EmployeeRestController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
|
||||
public class EmployeeControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@MockBean
|
||||
private EmployeeService service;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPostEmployee_thenCreateEmployee() throws Exception {
|
||||
Employee alex = new Employee("alex");
|
||||
given(service.save(Mockito.any())).willReturn(alex);
|
||||
|
||||
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(alex))).andExpect(status().isCreated()).andExpect(jsonPath("$.name", is("alex")));
|
||||
verify(service, VerificationModeFactory.times(1)).save(Mockito.any());
|
||||
reset(service);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception {
|
||||
Employee alex = new Employee("alex");
|
||||
Employee john = new Employee("john");
|
||||
Employee bob = new Employee("bob");
|
||||
|
||||
List<Employee> allEmployees = Arrays.asList(alex, john, bob);
|
||||
|
||||
given(service.getAllEmployees()).willReturn(allEmployees);
|
||||
|
||||
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(3))).andExpect(jsonPath("$[0].name", is(alex.getName()))).andExpect(jsonPath("$[1].name", is(john.getName())))
|
||||
.andExpect(jsonPath("$[2].name", is(bob.getName())));
|
||||
verify(service, VerificationModeFactory.times(1)).getAllEmployees();
|
||||
reset(service);
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
public class EmployeeRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestEntityManager entityManager;
|
||||
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Test
|
||||
public void whenFindByName_thenReturnEmployee() {
|
||||
Employee alex = new Employee("alex");
|
||||
entityManager.persistAndFlush(alex);
|
||||
|
||||
Employee found = employeeRepository.findByName(alex.getName());
|
||||
assertThat(found.getName()).isEqualTo(alex.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvalidName_thenReturnNull() {
|
||||
Employee fromDb = employeeRepository.findByName("doesNotExist");
|
||||
assertThat(fromDb).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindById_thenReturnEmployee() {
|
||||
Employee emp = new Employee("test");
|
||||
entityManager.persistAndFlush(emp);
|
||||
|
||||
Employee fromDb = employeeRepository.findById(emp.getId()).orElse(null);
|
||||
assertThat(fromDb.getName()).isEqualTo(emp.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvalidId_thenReturnNull() {
|
||||
Employee fromDb = employeeRepository.findById(-11l).orElse(null);
|
||||
assertThat(fromDb).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSetOfEmployees_whenFindAll_thenReturnAllEmployees() {
|
||||
Employee alex = new Employee("alex");
|
||||
Employee ron = new Employee("ron");
|
||||
Employee bob = new Employee("bob");
|
||||
|
||||
entityManager.persist(alex);
|
||||
entityManager.persist(bob);
|
||||
entityManager.persist(ron);
|
||||
entityManager.flush();
|
||||
|
||||
List<Employee> allEmployees = employeeRepository.findAll();
|
||||
|
||||
assertThat(allEmployees).hasSize(3).extracting(Employee::getName).containsOnly(alex.getName(), ron.getName(), bob.getName());
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.boot.Application;
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRepository;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = Application.class)
|
||||
@AutoConfigureMockMvc
|
||||
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
|
||||
// @TestPropertySource(locations = "classpath:application-integrationtest.properties")
|
||||
@AutoConfigureTestDatabase
|
||||
public class EmployeeRestControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Autowired
|
||||
private EmployeeRepository repository;
|
||||
|
||||
@After
|
||||
public void resetDb() {
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidInput_thenCreateEmployee() throws IOException, Exception {
|
||||
Employee bob = new Employee("bob");
|
||||
mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON).content(JsonUtil.toJson(bob)));
|
||||
|
||||
List<Employee> found = repository.findAll();
|
||||
assertThat(found).extracting(Employee::getName).containsOnly("bob");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployees_whenGetEmployees_thenStatus200() throws Exception {
|
||||
createTestEmployee("bob");
|
||||
createTestEmployee("alex");
|
||||
|
||||
// @formatter:off
|
||||
mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
|
||||
.andExpect(jsonPath("$", hasSize(greaterThanOrEqualTo(2))))
|
||||
.andExpect(jsonPath("$[0].name", is("bob")))
|
||||
.andExpect(jsonPath("$[1].name", is("alex")));
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private void createTestEmployee(String name) {
|
||||
Employee emp = new Employee(name);
|
||||
repository.saveAndFlush(emp);
|
||||
}
|
||||
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.internal.verification.VerificationModeFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.testing.Employee;
|
||||
import com.baeldung.boot.testing.EmployeeRepository;
|
||||
import com.baeldung.boot.testing.EmployeeService;
|
||||
import com.baeldung.boot.testing.EmployeeServiceImpl;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class EmployeeServiceImplIntegrationTest {
|
||||
|
||||
@TestConfiguration
|
||||
static class EmployeeServiceImplTestContextConfiguration {
|
||||
@Bean
|
||||
public EmployeeService employeeService() {
|
||||
return new EmployeeServiceImpl();
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
|
||||
@MockBean
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Employee john = new Employee("john");
|
||||
john.setId(11L);
|
||||
|
||||
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(john);
|
||||
Mockito.when(employeeRepository.findByName(alex.getName())).thenReturn(alex);
|
||||
Mockito.when(employeeRepository.findByName("wrong_name")).thenReturn(null);
|
||||
Mockito.when(employeeRepository.findById(john.getId())).thenReturn(Optional.of(john));
|
||||
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployees);
|
||||
Mockito.when(employeeRepository.findById(-99L)).thenReturn(Optional.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidName_thenEmployeeShouldBeFound() {
|
||||
String name = "alex";
|
||||
Employee found = employeeService.getEmployeeByName(name);
|
||||
|
||||
assertThat(found.getName()).isEqualTo(name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInValidName_thenEmployeeShouldNotBeFound() {
|
||||
Employee fromDb = employeeService.getEmployeeByName("wrong_name");
|
||||
assertThat(fromDb).isNull();
|
||||
|
||||
verifyFindByNameIsCalledOnce("wrong_name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidName_thenEmployeeShouldExist() {
|
||||
boolean doesEmployeeExist = employeeService.exists("john");
|
||||
assertThat(doesEmployeeExist).isEqualTo(true);
|
||||
|
||||
verifyFindByNameIsCalledOnce("john");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNonExistingName_thenEmployeeShouldNotExist() {
|
||||
boolean doesEmployeeExist = employeeService.exists("some_name");
|
||||
assertThat(doesEmployeeExist).isEqualTo(false);
|
||||
|
||||
verifyFindByNameIsCalledOnce("some_name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValidId_thenEmployeeShouldBeFound() {
|
||||
Employee fromDb = employeeService.getEmployeeById(11L);
|
||||
assertThat(fromDb.getName()).isEqualTo("john");
|
||||
|
||||
verifyFindByIdIsCalledOnce();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInValidId_thenEmployeeShouldNotBeFound() {
|
||||
Employee fromDb = employeeService.getEmployeeById(-99L);
|
||||
verifyFindByIdIsCalledOnce();
|
||||
assertThat(fromDb).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given3Employees_whengetAll_thenReturn3Records() {
|
||||
Employee alex = new Employee("alex");
|
||||
Employee john = new Employee("john");
|
||||
Employee bob = new Employee("bob");
|
||||
|
||||
List<Employee> allEmployees = employeeService.getAllEmployees();
|
||||
verifyFindAllEmployeesIsCalledOnce();
|
||||
assertThat(allEmployees).hasSize(3).extracting(Employee::getName).contains(alex.getName(), john.getName(), bob.getName());
|
||||
}
|
||||
|
||||
private void verifyFindByNameIsCalledOnce(String name) {
|
||||
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findByName(name);
|
||||
Mockito.reset(employeeRepository);
|
||||
}
|
||||
|
||||
private void verifyFindByIdIsCalledOnce() {
|
||||
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findById(Mockito.anyLong());
|
||||
Mockito.reset(employeeRepository);
|
||||
}
|
||||
|
||||
private void verifyFindAllEmployeesIsCalledOnce() {
|
||||
Mockito.verify(employeeRepository, VerificationModeFactory.times(1)).findAll();
|
||||
Mockito.reset(employeeRepository);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.boot.testing;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
class JsonUtil {
|
||||
static byte[] toJson(Object object) throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
return mapper.writeValueAsBytes(object);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user