Transferred from: http://blog.csdn.net/yin_jw/article/details/24726941
Category: Spring Open source Framework 2014-04-29 17:01 1786 people read reviews (2) favorite reports Spring comes with mock test contro
Preparing the SPRINGMVC Environment
Note: Using a mock test requires the introduction of the Spring-test package
Base class: Load Configuration file
[Java]View Plaincopy
- Package Com.wyy.snail.user.controller;
- Import Org.junit.runner.RunWith;
- Import org.springframework.test.context.ContextConfiguration;
- Import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
- Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- @ContextConfiguration (locations = { "Classpath:config/spring/spring-*.xml",
- "Classpath:config/spring/service-mybatis.xml"})
- @RunWith (Springjunit4classrunner. Class)
- Public class Basecontrollertest extends Abstracttransactionaljunit4springcontexttests {
- }
Test class: Specific business
[Java]View Plaincopy
- Package Com.wyy.snail.user.controller;
- Import java.io.IOException;
- Import Org.junit.Test;
- Import Org.springframework.mock.web.MockHttpServletRequest;
- Import Org.springframework.mock.web.MockHttpServletResponse;
- Public class Logincontrollertest extends Basecontrollertest {
- @Test
- public void Testlogin () throws IOException {
- Logincontroller Logincontroller = (logincontroller) This.applicationContext.getBean ("Logincontroller");
- Mockhttpservletrequest request = new Mockhttpservletrequest ();
- Mockhttpservletresponse response = new Mockhttpservletresponse ();
- Request.setmethod ("POST");
- Request.addparameter ("username", "AA");
- Request.addparameter ("password", "BB");
- Logincontroller.login (Request, Response, " ", "" , "");
- }
Spring comes with mock test controller