在JUnit5中的MockMVC.perform期间预期异常

huangapple go评论79阅读模式
英文:

Expecting exception during MockMVC.perform in JUnit5

问题

这可能是一个重复的问题。但我已经阅读了大约10-15个相关帖子和相关回复,但没有解决我的问题。我面临的问题如下所示:
我有一个带有自定义ApplicationException的Spring Rest控制器类。
我为我的控制器编写了一个Junit,以下是我遇到问题的代码片段。

this.mockMvc.perform(MockMvcRequestBuilders.post(url)
        .contentType(MediaType.APPLICATION_JSON)
        .content(new ObjectMapper().writeValueAsString(requestObject))
        .headers(header)
        .accept(MediaType.APPLICATION_JSON)
    )
    .andDo(print())
    .andExpect(status().is4xxClientError());

当我执行测试方法时,我看到代码抛出了ApplicationException,但Junit失败了,我在Junit控制台中看到的是

"org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.abc.pmr.case.exception.ApplicationException at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
...
...
Caused by: com.abc.pmr.case.exception.ApplicationException
    at com.abc.pmr.case.exception.ApplicationException

我还尝试了 .andExpect(mvcresult -> assertTrue(mvcresult.getResolvedException() instanceof ApplicationException));,但也没有帮助。

尽管在此类中的所有成功场景测试用例都已成功通过,但Junit在上述异常的情况下失败了,而我想通过预期的异常(ApplicationException)传递Junit。
注意:在此类中的所有成功场景测试用例都已成功通过。

非常感谢您在这里提供的任何帮助。

英文:

This may be a repeated question. But I have gone through some 10-15 related posts and associated responses which have not resolved my issue. The issue that I am facing is as here below
I have a SpringRest controller class with custom ApplicationException.
I have written a Junit for my controller and here below is the snippet where I am facing issue.

this.mockMvc.perform(MockMvcRequestBuilders.post(url)
		.contentType(MediaType.APPLICATION_JSON)
		.content(new ObjctMapper().writeValueAsString(requestObject)
		.headers(header)
		.accept(MediaType.APPLICATION_JSON)
	)
	.andDo(print())
	.andExpect(status().is4xxClientError());

When I execute the test method, I see that ApplicationException is thrown from the code, but the Junit fails and what I see in Junit Console is

"org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.abc.pmr.case.exception.ApplicationException at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
...
...
Caused by: com.abc.pmr.case.exception.ApplicationException
	at com.abc.pmr.case.exception.ApplicationException

I tried with .andExpect(mvcresult -> assertTrue(mvcresult.getResolvedException() instanceof ApplicationException)); as well.
But that did not help either.

The Junit fails with the above said exception, while I want to pass the Junit with the expected Exception as ApplicationException.
Note: All the success scenario testcases in this class are passing successfully.

Any help here would be much appreciated.

答案1

得分: 1

我认为您的mockMvc设置对您的情况不完全正确。
确保您已将异常处理程序设置为您的mockMvc
您可以在这里找到更多信息。

因此,当您的环境配置正确时,您将能够测试此逻辑。

英文:

I suppose that your mockMvc setup is not completely correct for your case.
Make sure that you have set exception handler to your mockMvc.
You can find more info about this here.

So, when your environment will be configured correct you will have an ability test this logic.

huangapple
  • 本文由 发表于 2020年10月11日 22:15:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64304992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定