스프링 컨트롤러 메서드에 대한 junit 테스트를 만들려고하는데 다음 오류가 계속 발생합니다.봄철 JUnit 테스트
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message,
your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:123)
필요한 두 가지를 추가했습니다. (각각 별도로 시도했습니다) 현재 내 web.xml에는
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
가 포함되어 있으며 테스트하려는 메소드는
@Controller
@RemotingDestination
public class MyController {
public Response foo()
{
//...
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession httpSession = attr.getRequest().getSession(true);
//...
}
행을 따르고 my junit 테스트는 단순히 myController.foo()를 호출하고 응답을 확인합니다. 그래서이 문제를 해결하기 위해 메소드에 전달할 mock 객체를 만들 수 없습니다.
그래서 내 질문에, 거기에 일부 구성 또는 트릭이 내 컨트롤러 메서드를 리팩터링하지 않고도이 실행하게됩니다 우연히 발견 할 수 있습니까?
+1 Btw, 샘플 코드에 유형을 추가해야합니다. –
고마워, 고쳐! –
감사합니다. 미래에이 문제를 읽는 사람들에게 내 문제를 해결하기 위해 무엇을했는지 명확히하기 위해 - @SessionAttributes ("mySessionAttribute") 공용 클래스 AccountController {...}'를 사용했고 내 메소드는'public Response foo (@ModelAttribute ("mySessionAttribute") Object myObject) {...}' 장기적으로 내 상황에서 작동하는지 여부는 아직 알 수 없지만 현재로서는 효과가 있습니다. 희망이 다른 사람을 도와줍니다. – user960564