<bdo id='eTYxq'></bdo><ul id='eTYxq'></ul>
    1. <tfoot id='eTYxq'></tfoot>
      <i id='eTYxq'><tr id='eTYxq'><dt id='eTYxq'><q id='eTYxq'><span id='eTYxq'><b id='eTYxq'><form id='eTYxq'><ins id='eTYxq'></ins><ul id='eTYxq'></ul><sub id='eTYxq'></sub></form><legend id='eTYxq'></legend><bdo id='eTYxq'><pre id='eTYxq'><center id='eTYxq'></center></pre></bdo></b><th id='eTYxq'></th></span></q></dt></tr></i><div id='eTYxq'><tfoot id='eTYxq'></tfoot><dl id='eTYxq'><fieldset id='eTYxq'></fieldset></dl></div>

      1. <small id='eTYxq'></small><noframes id='eTYxq'>

      2. <legend id='eTYxq'><style id='eTYxq'><dir id='eTYxq'><q id='eTYxq'></q></dir></style></legend>

        如何对 Spring MVC 注释控制器进行单元测试?

        时间:2023-09-28

        <i id='rcoai'><tr id='rcoai'><dt id='rcoai'><q id='rcoai'><span id='rcoai'><b id='rcoai'><form id='rcoai'><ins id='rcoai'></ins><ul id='rcoai'></ul><sub id='rcoai'></sub></form><legend id='rcoai'></legend><bdo id='rcoai'><pre id='rcoai'><center id='rcoai'></center></pre></bdo></b><th id='rcoai'></th></span></q></dt></tr></i><div id='rcoai'><tfoot id='rcoai'></tfoot><dl id='rcoai'><fieldset id='rcoai'></fieldset></dl></div>
        <tfoot id='rcoai'></tfoot>

            <small id='rcoai'></small><noframes id='rcoai'>

              <tbody id='rcoai'></tbody>
            1. <legend id='rcoai'><style id='rcoai'><dir id='rcoai'><q id='rcoai'></q></dir></style></legend>

                  <bdo id='rcoai'></bdo><ul id='rcoai'></ul>
                • 本文介绍了如何对 Spring MVC 注释控制器进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在关注 Spring 2.5 教程,同时尝试将代码/设置更新到 Spring 3.0.

                  I am following a Spring 2.5 tutorial and trying, at the same time, updating the code/setup to Spring 3.0.

                  Spring 2.5 我有 HelloController(供参考):

                  In Spring 2.5 I had the HelloController (for reference):

                  public class HelloController implements Controller {
                      protected final Log logger = LogFactory.getLog(getClass());
                      public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                          logger.info("Returning hello view");
                          return new ModelAndView("hello.jsp");
                      }
                  }
                  

                  HelloController 的 JUnit 测试(供参考):

                  And a JUnit test for the HelloController (for reference):

                  public class HelloControllerTests extends TestCase {
                      public void testHandleRequestView() throws Exception{
                          HelloController controller = new HelloController();
                          ModelAndView modelAndView = controller.handleRequest(null, null);
                          assertEquals("hello", modelAndView.getViewName());
                      }
                  }
                  

                  但现在我将控制器更新为 Spring 3.0,它现在使用注释(我还添加了一个 消息):

                  But now I updated the controller to Spring 3.0, and it now uses annotations (I also added a message):

                  @Controller
                  public class HelloController {
                      protected final Log logger = LogFactory.getLog(getClass());
                      @RequestMapping("/hello")
                      public ModelAndView handleRequest() {
                          logger.info("Returning hello view");
                          return new ModelAndView("hello", "message", "THIS IS A MESSAGE");
                      }
                  }
                  

                  知道我使用的是 JUnit 4.9,有人能解释一下如何对最后一个控制器进行单元测试吗?

                  Knowing that I am using JUnit 4.9, can some one explain me how to unit test this last controller?

                  推荐答案

                  基于注解的 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.

                  这篇关于如何对 Spring MVC 注释控制器进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:自动装配同一类的两个不同的bean 下一篇:@OneToOne(optional=false) 和 @JoinColumn(nullable=false) 一起

                  相关文章

                  最新文章

                • <legend id='czDeT'><style id='czDeT'><dir id='czDeT'><q id='czDeT'></q></dir></style></legend>

                  <small id='czDeT'></small><noframes id='czDeT'>

                      <bdo id='czDeT'></bdo><ul id='czDeT'></ul>
                      <i id='czDeT'><tr id='czDeT'><dt id='czDeT'><q id='czDeT'><span id='czDeT'><b id='czDeT'><form id='czDeT'><ins id='czDeT'></ins><ul id='czDeT'></ul><sub id='czDeT'></sub></form><legend id='czDeT'></legend><bdo id='czDeT'><pre id='czDeT'><center id='czDeT'></center></pre></bdo></b><th id='czDeT'></th></span></q></dt></tr></i><div id='czDeT'><tfoot id='czDeT'></tfoot><dl id='czDeT'><fieldset id='czDeT'></fieldset></dl></div>
                    1. <tfoot id='czDeT'></tfoot>