1. Sample Controller 생성
@RestController
@Slf4j
@RequestMapping("/sample-board")
public class SampleBoardController {
private SampleBoardService sampleBoardService;
public SampleBoardController(SampleBoardService sampleBoardService) {
this.sampleBoardService = sampleBoardService;
}
@GetMapping
public List<SampleBoard> getSampleBoards() { ///sample-board
return sampleBoardService.getSampleBoards();
}
}
2. Test 클래스 생성
* mockMvc 객체를 Autowired 하여 url, header 등을 입력하여 전송한다.
@AutoConfigureMockMvc
@SpringBootTest
class SampleBoardControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
void getSampleBoards() throws Exception{
HttpHeaders httpHeaders = new HttpHeaders();
mockMvc.perform(get("/sample-board")
.contentType(MediaType.APPLICATION_JSON)
.headers(httpHeaders))
.andExpect(status().isOk())
.andDo(print());
}
}
'개발 > 스프링 프레임워크' 카테고리의 다른 글
[SpringBoot3 + Log4j2] Routing 활용하여 요청별 로그파일 생성 (0) | 2023.07.26 |
---|---|
[Spring Boot] JPA, AuditorAware 사용하여 사용자정보 자동 입력 (1) | 2023.07.12 |
[Spring Boot] H2 + JPA 세팅 (1) | 2022.07.21 |
[Spring Secutiry] Invalid CSRF token found for... (0) | 2020.09.15 |
[스프링 프레임워크] 스프링MVC 프로젝트 + 톰캣 연결 (0) | 2017.12.01 |