Spring comes with mock test controller

Source: Internet
Author: User

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
  1. Package Com.wyy.snail.user.controller;
  2. Import Org.junit.runner.RunWith;
  3. Import org.springframework.test.context.ContextConfiguration;
  4. Import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
  5. Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6. @ContextConfiguration (locations = { "Classpath:config/spring/spring-*.xml",
  7. "Classpath:config/spring/service-mybatis.xml"})
  8. @RunWith (Springjunit4classrunner. Class)
  9. Public class Basecontrollertest extends Abstracttransactionaljunit4springcontexttests {
  10. }

Test class: Specific business

[Java]View Plaincopy
  1. Package Com.wyy.snail.user.controller;
  2. Import java.io.IOException;
  3. Import Org.junit.Test;
  4. Import Org.springframework.mock.web.MockHttpServletRequest;
  5. Import Org.springframework.mock.web.MockHttpServletResponse;
  6. Public class Logincontrollertest extends Basecontrollertest {
  7. @Test
  8. public void Testlogin () throws IOException {
  9. Logincontroller Logincontroller = (logincontroller) This.applicationContext.getBean ("Logincontroller");
  10. Mockhttpservletrequest request = new Mockhttpservletrequest ();
  11. Mockhttpservletresponse response = new Mockhttpservletresponse ();
  12. Request.setmethod ("POST");
  13. Request.addparameter ("username", "AA");
  14. Request.addparameter ("password", "BB");
  15. Logincontroller.login (Request, Response, " ", "" , "");
  16. }

Spring comes with mock test controller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.