JUint 테스트를 위한 클래스
package com.cwpark.library;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequiredArgsConstructor
public class TestController {
private final TestService testService;
@GetMapping("/test/{string}")
public Map<String, Object> getString(@PathVariable("string") String string) {
Map<String, Object> result = new HashMap<>();
if(testService.getString(string).equals("success")) {
result.put("success", "Y");
} else {
result.put("success", "N");
}
return result;
}
@PostMapping("/test")
public Map<String, Object> postString(@RequestBody Map<String, Object> map) {
Map<String, Object> result = new HashMap<>();
if(testService.postString(map.get("string").toString()).equals("success")) {
result.put("success", "Y");
} else {
result.put("success", "N");
}
return result;
}
}
package com.cwpark.library;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class TestService {
private final TestDao testDao;
public String getString(String string) {
return testDao.getString(string);
}
public String postString(String string) {
return testDao.postString(string);
}
}
package com.cwpark.library;
import org.springframework.stereotype.Service;
@Service
public class TestDao {
public String getString(String string) {
if(string.equals("string")) {
return "success";
} else {
return "fail";
}
}
public String postString(String string) {
if(string.equals("string")) {
return "success";
} else {
return "fail";
}
}
}
JUint 사용
package com.cwpark.library;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(TestController.class)
@MockBean(JpaMetamodelMappingContext.class)
class TestControllerTest {
@Autowired
MockMvc mockMvc;
@MockBean
TestService testService;
@Test
@DisplayName("GET 테스트")
@WithMockUser
void getStringTest() throws Exception {
given(testService.getString("string")).willReturn("success");
String string = "string";
mockMvc.perform(get("/test/" + string))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value("Y"))
.andDo(print());
verify(testService).getString("string");
}
@Test
@DisplayName("POST 테스트")
@WithMockUser
void postStringTest() throws Exception {
given(testService.postString("string")).willReturn("success");
Map<String, Object> map = new HashMap<>();
map.put("string", "string");
String json = new ObjectMapper().writeValueAsString(map);
mockMvc.perform(post("/test")
.content(json)
.contentType(MediaType.APPLICATION_JSON)
.with(csrf()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value("Y"))
.andDo(print());
verify(testService).postString("string");
}
@Test
@DisplayName("해당 테스트를 실행하지 않겠다")
@Disabled
void disable() {
}
}
package com.cwpark.library;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
@ExtendWith(SpringExtension.class)
@Import({TestService.class})
class TestServiceTest {
@MockBean
TestDao testDao;
@Autowired
TestService testService;
@Test
public void getStringTest() {
Mockito.when(testDao.getString("string"))
.thenReturn("success");
String string = testService.getString("string");
Assertions.assertEquals(string, "success");
verify(testDao).getString("string");
}
@Test
public void postStringTest() {
Mockito.when(testDao.postString("string"))
.thenReturn("success");
String string = testService.postString("string");
Assertions.assertEquals(string, "success");
verify(testDao).postString("string");
}
}