更新时间:2023-09-17 11:33:22
基于注解的 Spring MVC 的一个优点是可以直接测试它们,如下所示:
One advantage of annotation-based Spring MVC is that they can be tested in a straightforward manner, like so:
import org.junit.Test;
import org.junit.Assert;
import org.springframework.web.servlet.ModelAndView;
public class HelloControllerTest {
@Test
public void testHelloController() {
HelloController c= new HelloController();
ModelAndView mav= c.handleRequest();
Assert.assertEquals("hello", mav.getViewName());
...
}
}
这种方法有什么问题吗?
Is there any problem with this approach?
对于更高级的集成测试,有一个 Spring 文档中的参考 到 org.springframework.mock.web.
For more advanced integration testing, there is a reference in Spring documentation to the org.springframework.mock.web.