티스토리 뷰

MockMvc 주입 오류

컨트롤러 단순 동작 테스트 중 MockMvc가 주입되지 않아 삽질을 했다...

오류 상황

Could not autowire, No beans of ‘MockMvc’ type found라는 오류 메시지가 표시된다..

찾아보니 IntelliJ에서 발생하는 단순 오류로 보인다.

Inspection 'Incorrect injection point autowiring in Spring bean components' optioins - Supress for field를 설정해주면 아래와 같이

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
private MockMvc mvc;

@SuppressWarnings 어노테이션이 붙으며 특정 오류 지점을 무시할 수 있게 된다.

 

IntelliJ를 업데이트하면 된다는 사람도 있는데, 일단 위 방법으로 해결했고 정상 동작하는 것을 확인했다.

 

 


import할 것

controller 테스트 코드를 짤 때 MockMvc의 get, post 메소드의 content(), status(), andExpect() 등을 쉽게 import하기 위해 아래 코드를 선언해주고 시작하자!

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

 

jsonPath 사용시 is로 비교하기

.andExpect(jsonPath("$.name", is(name)))
.andExpect(jsonPath("$.amount", is(amount)));

여기서 is를 사용하기 위해 아래 코드를 직접 import해줘야 한다.

import static org.hamcrest.Matchers.is;

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함