Spring boot 튜토리얼 (4) - Testing
14. Unit Testing
1) Service Layer testing
(1) Service Test 생성
(2) Service(Test)
@SpringBootTest
class DepartmentServiceTest {
@Autowired
private DepartmentService departmentService;
@MockBean
private DepartmentRepository departmentRepository;
@BeforeEach
void setUp() {
Department department =
Department.builder()
.departmentName("IT")
.departmentAddress("SEOUL")
.departmentCode("IT-06")
.departmentId(1L)
.build();
Mockito.when(departmentRepository.findByDepartmentNameIgnoreCase("IT"))
.thenReturn(department);
}
@Test
@DisplayName("Get Data based on Valid Department Name")
// @Disabled
public void whenValidDepartmentName_thenDepartmentShouldFound() {
String departmentName = "IT";
Department found = departmentService.fetchDepartmentByName(departmentName);
assertEquals(departmentName, found.getDepartmentName());
}
}
2) Repository Layer testing
(1) Repository Test 생성
(2) Repository(Test)
@DataJpaTest
class DepartmentRepositoryTest {
@Autowired
private DepartmentRepository departmentRepository;
@Autowired
private TestEntityManager entityManager;
@BeforeEach
void setUp() {
Department department =
Department.builder()
.departmentName("Mechanical Engineering")
.departmentCode("ME-011")
.departmentAddress("Busan")
.build();
entityManager.persist(department);
}
@Test
public void whenFindById_thenReturnDepartment() {
Department department = departmentRepository.findById(1L).get();
assertEquals(department.getDepartmentName(), "Mechanical Engineering");
}
}
3) Controller Layer testing
(1) Controller Test 생성
(2) Controller(Test)
@WebMvcTest(DepartmentController.class)
class DepartmentControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private DepartmentService departmentService;
private Department department;
@BeforeEach
void setUp() {
department = Department.builder()
.departmentAddress("JEJU")
.departmentCode("IT-06")
.departmentName("IT")
.departmentId(1L)
.build();
}
@Test
void saveDepartment() throws Exception {
Department inputDepartment = Department.builder()
.departmentAddress("JEJU")
.departmentCode("IT-06")
.departmentName("IT")
.build();
Mockito.when(departmentService.saveDepartment(inputDepartment))
.thenReturn(department);
mockMvc.perform(MockMvcRequestBuilders.post("/departments")
.contentType(MediaType.APPLICATION_JSON)
.content("{\n" +
" \"departmentName\": \"IT\",\n" +
" \"departmentAddress\": \"JEJU\",\n" +
" \"departmentCode\": \"IT-06\"\n" +
"}"))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
void fetchDepartmentById() throws Exception {
Mockito.when(departmentService.fetchDepartmentById(1L))
.thenReturn(department);
mockMvc.perform(MockMvcRequestBuilders.get("/departments/1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.departmentName")
.value(department.getDepartmentName()));
}
}
15. Adding Config in properties file
application.properties 적용
// application.properties
welcome.message = Welcome to Daily Code Buffer!!
@RestController
public class HelloController {
@Value("${welcome.message}")
private String welcomeMessage;
@GetMapping("/")
public String hellowWorld() {
return welcomeMessage;
}
}
16. Adding application.yml file
application.yml 적용
server:
port: 8082
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
url: jdbc:mysql://localhost:3306/dcbapp
password: password
jpa:
hibernate:
ddl-auto: update
show-sql: 'true'
welcome:
message: Welcome to Daily Code Buffer!!
참고 : Spring Boot Tutorial | Full In-depth Course - Daily Code Buffer
Author And Source
이 문제에 관하여(Spring boot 튜토리얼 (4) - Testing), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@kimkevin90/Spring-boot-튜토리얼-4-Testing저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)