In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to do JUnit unit testing for Action in Spring3 MVC. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
After using spring3 MVC, it is also very convenient to do unit tests for action. I have never written unit tests for action before, but it is different and convenient, so be sure to write.
The JUnitActionBase class is the parent class of all JUnit test classes
Package test; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.BeforeClass; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ModelAndView Import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter; import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping; / * description: the base class used by JUnit to test action * * @ author Zhao Lei * @ version creation time: 2011-2-2 pm 10:27:03 * / public class JUnitActionBase {private static HandlerMapping handlerMapping; private static HandlerAdapter handlerAdapter / * read spring3 MVC configuration file * / @ BeforeClass public static void setUp () {if (handlerMapping = = null) {String [] configs = {"file:src/springConfig/springMVC.xml"}; XmlWebApplicationContext context = new XmlWebApplicationContext (); context.setConfigLocations (configs) MockServletContext msc = new MockServletContext (); context.setServletContext (msc); context.refresh (); msc.setAttribute (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); handlerMapping = (HandlerMapping) context.getBean (DefaultAnnotationHandlerMapping.class); handlerAdapter = (HandlerAdapter) context.getBean (context.getBeanNamesForType (AnnotationMethodHandlerAdapter.class) [0]) }} / * action * * @ param request * @ param response * @ return * @ throws Exception * / public ModelAndView excuteAction (HttpServletRequest request, HttpServletResponse response) throws Exception {HandlerExecutionChain chain = handlerMapping.getHandler (request) Final ModelAndView model = handlerAdapter.handle (request, response, chain.getHandler (); return model;}}
More information about Spring
Spring Forum http://www.itchm.com/forum-59-1.html
This is a JUnit test class, we can new Request objects to participate in the test, so convenient. Specify the URL to access to the request, and you can request the target Action.
Package test.com.app.user; import org.junit.Assert; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.web.servlet.ModelAndView; import test.JUnitActionBase / * Note: an example of testing OrderAction * * @ author Zhao Lei * @ version creation time: 2011-2-2 pm 10:26:55 * / public class TestOrderAction extends JUnitActionBase {@ Test public void testAdd () throws Exception {MockHttpServletRequest request = new MockHttpServletRequest (); MockHttpServletResponse response = new MockHttpServletResponse (); request.setRequestURI ("/ order/add") Request.addParameter ("id", "1002"); request.addParameter ("date", "2010-12-30"); request.setMethod ("POST"); / / execute action final ModelAndView mav = this.excuteAction (request, response) for URI; / / Assert logic Assert.assertEquals ("order/add", mav.getViewName ()) String msg= (String) request.getAttribute ("msg"); System.out.println (msg);}}
Need to be clear: since the most desired version of Spring (Test) 3.0.5 does not support annotated context file injection of @ ContextConfiguration, you still need to write a setUp to handle it, otherwise the loading process similar to Tiles will have errors because there is no ServletContext. Version 3.1 should have a better solution.
Thank you for reading! This is the end of this article on "how to do JUnit unit testing for Action in Spring3 MVC". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.