Java – write JUnit tests for @ exceptionhandler
I am writing a rest service using spring MVC This is the outline of the course:
@Controller
public class MyController{
@RequestMapping(..)
public void myMethod(...) throws NotAuthorizedException{...}
@ExceptionHandler(NotAuthorizedException.class)
@ResponseStatus(value=HttpStatus.UNAUTHORIZED,reason="blah")
public void handler(...){...}
}
I wrote my unit tests and released here using the design The basic tests are as follows:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(....)
public class mytest{
MockHttpServletRequest requestMock;
MockHttpServletResponse responseMock;
AnnotationMethodHandlerAdapter handlerAdapter;
@Before
public void setUp() {
requestMock = new MockHttpServletRequest();
requestMock.setContentType(MediaType.APPLICATION_JSON_VALUE);
requestMock.addHeader(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_VALUE);
responseMock = new MockHttpServletResponse();
handlerAdapter = new AnnotationMethodHandlerAdapter();
}
@Test
public void testExceptionHandler(){
// setup ....
handlerAdapter.handle(...);
// verify
// I would like to do the following
assertThat(responseMock.getStatus(),is(HttpStatus.UNAUTHORIZED.value()));
}
}
However, the call to handle throws notauthorizedexception I have read this design so that I can unit test the method to throw the appropriate exception, but I want to write an automatic test. The framework handles the exception correctly, and the tested class implements the handler correctly Is there any way to do this?
Please note that I cannot access the actual code where I can publish
In addition, I am interested in spring 3.0 5 or 3.1 Limited (unfortunately)
Solution
Consider using spring 3.2 and its MVC test framework
import static org.springframework.test.web.servlet.setup.mockmvcBuilders.*;
import static org.springframework.test.web.servlet.request.mockmvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.mockmvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml")
public class WebMvcTest {
@Autowired
private WebApplicationContext wac;
private mockmvc mockmvc;
@Before
public void setup() {
this.mockmvc = mockmvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void getFoo() throws Exception {
this.mockmvc.perform(
get("/testx")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
)
.andExpect(status().isUnauthorized());
}
}
Controller code
@Controller
public class MyController {
public class MyException extends RuntimeException {
};
@RequestMapping("/testx")
public void myMethod() {
throw new MyException();
}
@ExceptionHandler(MyException.class)
@ResponseStatus(value = HttpStatus.UNAUTHORIZED,reason = "blah")
public void handler() {
System.out.println("handler processed");
}
}
The test went well
Disclaimer: at present, I am noob in the spring MVC test, which is actually my first test UPD: thanks for Drake for the correction
