测试Spring MVC注释映射 [英] Testing Spring MVC annotation mappings

查看:104
本文介绍了测试Spring MVC注释映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With Spring MVC, you can specify that a particular URL will handled by a particular method, and you can specify that particular parameters will map to particular arguments, like so:

@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请求将正确调用此方法。换句话说,如果我删除或更改任何注释,我想要一个会破坏的测试。有没有办法做到这一点?

This is all well and good, but now I want to test that an http request with an imageId parameter will invoke this method correctly. In other words, I want a test that will break if I remove or change any of the annotations. Is there a way to do this?

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

It is easy to test that getImage works correctly. I could just create an ImageController and invoke getImage with appropriate arguments. However, this is only one half of the test. The other half of the test must be whether getImage() will be invoked by the Spring framework when an appropriate HTTP request comes in. I feel like I also need a test for this part, especially as my @RequestMapping annotations become more complex and invoke complex parameter conditions.

如果我删除第4行,你能否告诉我一个测试会破坏, @RequestMapping(getImage)

Could you show me a test that will break if I remove line 4, @RequestMapping("getImage")?

推荐答案

你可以使用 AnnotationMethodHandlerAdapter 及其以编程方式处理方法。这将解析给定请求的方法并执行它。不幸的是,这有点间接。实际上在AMHA中有一个名为 ServletHandlerMethodResolver 的私有类,它负责解析给定请求的方法。我刚刚就该主题提交了改进请求,因为我真的很想看到这个也可以。

You could use AnnotationMethodHandlerAdapter and its handle method programmatically. This will resolve the method for the given request and execute it. Unfortunately this is a little indirect. Actually there is a private class called ServletHandlerMethodResolver in AMHA that is responsible for just resolving the method for a given request. I just filed a request for improvement on that topic, as I really would like to see this possible, too.

在此期间你可以使用eg EasyMock 创建控制器类的模拟,期望调用给定的方法并将模拟交给手柄

In the meantime you could use e.g. EasyMock to create a mock of your controller class, expect the given method to be invoked and hand that mock to 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);
  }
}

问候,
Ollie

Regards, Ollie

这篇关于测试Spring MVC注释映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆