测试弹簧 MVC 注释映射

2022-09-02 10:26:27

使用Spring MVC,您可以指定特定URL将由特定方法处理,并且可以指定特定参数将映射到特定参数,如下所示:

@Controller
public class ImageController {

   @RequestMapping("/getImage")
   public String getImage( @RequestParam("imageId") int imageId, Map<String,Object> model ) {
      model.put("image",ImageService.getImage(imageId));
   }

}

这一切都很好,但现在我想测试一个带有imageId参数的http请求将正确调用此方法。换句话说,我想要一个测试,如果我删除或更改任何注释,该测试将中断。有没有办法做到这一点?

很容易测试getImage是否正常工作。我可以创建一个 ImageController 并使用适当的参数调用 getImage。但是,这只是测试的一半。测试的另一半必须是当适当的HTTP请求进入时,Spring框架是否会调用getImage()。我觉得我还需要对这部分进行测试,特别是当我的注释变得更加复杂并调用复杂的参数条件时。@RequestMapping

你能给我看一个测试,如果我删除第4行,它会中断吗?@RequestMapping("getImage")


答案 1

您可以以编程方式使用NotscriptMethodHandlerAdapter及其方法。这将解析给定请求的方法并执行它。不幸的是,这有点间接。实际上,AMHA中有一个私有类,负责仅解析给定请求的方法。我刚刚就该主题提出了改进请求,因为我真的希望看到这成为可能。handleServletHandlerMethodResolver

同时,您可以使用例如 EasyMock 来创建控制器类的模拟,期望调用给定的方法并将该模拟交给 。handle

控制器:

@Controller
public class MyController {

  @RequestMapping("/users")
  public void foo(HttpServletResponse response) {

    // your controller code
  }
}

测试:

public class RequestMappingTest {

  private MockHttpServletRequest request;
  private MockHttpServletResponse response;
  private MyController controller;
  private AnnotationMethodHandlerAdapter adapter;


  @Before
  public void setUp() {

    controller = EasyMock.createNiceMock(MyController.class);

    adapter = new AnnotationMethodHandlerAdapter();
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
  }


  @Test
  public void testname() throws Exception {

    request.setRequestURI("/users");

    controller.foo(response);
    EasyMock.expectLastCall().once();
    EasyMock.replay(controller);

    adapter.handle(request, response, controller);

    EasyMock.verify(controller);
  }
}

问候, 奥利


答案 2

Ollie的解决方案涵盖了测试注释的特定示例,但是如何测试所有其他各种Spring MVC注释的更广泛问题呢?我的方法(可以很容易地扩展到其他注释)是

import static org.springframework.test.web.ModelAndViewAssert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({/* include live config here
    e.g. "file:web/WEB-INF/application-context.xml",
    "file:web/WEB-INF/dispatcher-servlet.xml" */})
public class MyControllerIntegrationTest {

    @Inject
    private ApplicationContext applicationContext;

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private HandlerAdapter handlerAdapter;
    private MyController controller;

    @Before
    public void setUp() {
       request = new MockHttpServletRequest();
       response = new MockHttpServletResponse();
       handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
       // I could get the controller from the context here
       controller = new MyController();
    }

    @Test
    public void testFoo() throws Exception {
       request.setRequestURI("/users");
       final ModelAndView mav = handlerAdapter.handle(request, response, 
           controller);
       assertViewName(mav, null);
       assertAndReturnModelAttributeOfType(mav, "image", Image.class);
    }
}

我还写了一篇关于集成测试Spring MVC注释的博客文章


推荐