我正在尝试为使用Spring MVC构建的REST API编写集成测试。以下是我的控制器类。
@RestController
@RequestMapping("/api")
public class ZoningToolRestController {
@Autowired
ImageProcessingService service;
@RequestMapping(value = "/page/{index}", produces = "image/png", method = RequestMethod.GET)
public byte[] getPage(@PathVariable("index") int index) throws InvalidIndexException, IOException {
return service.getPage(index);
}
@ExceptionHandler
public ResponseEntity<Map<String, Object>> handleException(Exception e) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("error", e.getMessage());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<Map<String, Object>>(map, headers, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
以下是我的考试班
@RunWith(SpringRunner.class)
@SpringBootTest
public class ZoningToolRestControllerTests {
private MockMvc mvc;
@Autowired
private ZoningToolRestController zoningToolRestController;
@Before
public void setup() {
this.mvc = MockMvcBuilders.standaloneSetup(zoningToolRestController)
.setHandlerExceptionResolvers(getExceptionResolver())
.build();
}
private HandlerExceptionResolver getExceptionResolver() {
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver();
exceptionResolver.setMappedHandlerClasses(InvalidIndexException.class, ExceptionHandler.class);
return exceptionResolver;
}
/**
* Test to check if image is returned in case of valid call
*
* @throws Exception
*/
@Test
public void givenDocument_whenGetValidPage_thenSuccess() throws Exception {
mvc.perform(get("/api/page/1").accept(MediaType.IMAGE_PNG)).andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.IMAGE_PNG));
}
/**
* Test to check if server throws an error when wrong page is passed
*
* @throws Exception
*/
@Test
public void givenDocument_whenGetWrongPage_thenError() throws Exception {
mvc.perform(get("/api/page/11").accept(MediaType.IMAGE_PNG)).andExpect(status().isInternalServerError())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
}
}
现在,当我尝试运行此单元测试时,第二个单元测试失败,因为它期望InvalidIndexException时遇到NestedSerlvletException
我在做什么错了?
更新 堆栈跟踪
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is in.co.dhdigital.zoningtool.processors.InvalidIndexException: Invalid index passed.
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:182)
at in.co.dhdigital.zoningtool.test.controllers.ZoningToolRestControllerTests.givenDocument_whenGetWrongPage_thenError(ZoningToolRestControllerTests.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: in.co.dhdigital.zoningtool.processors.InvalidIndexException: Invalid index passed.
at in.co.dhdigital.zoningtool.processors.impl.TiffImageProcessor.getPage(TiffImageProcessor.java:53)
at in.co.dhdigital.zoningtool.services.ImageProcessingService.getPage(ImageProcessingService.java:32)
at in.co.dhdigital.zoningtool.controllers.ZoningToolRestController.getPage(ZoningToolRestController.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
... 41 more
答案 0 :(得分:0)
如果您需要在测试期间声明Exception,请对测试方法使用以下注释。
@Test(expected = InvalidIndexException.class)
您可以在这里详细了解以下内容:https://www.baeldung.com/junit-assert-exception
答案 1 :(得分:0)
/ ** *测试以检查通过错误页面时服务器是否引发错误 * * @throws异常 * /
def stripper(delete_list):
def delete(item):
nonlocal delete_list
for rm in delete_list:
item = item.lstrip(rm)
return item
return delete
df['Names'] = df['Names'].apply(stripper(['AL', 'THE'])