Spring JUnit Test

Source: Internet
Author: User
Tags http request

Use spring's mock classes for unit testing:

The spring Framework provides a large number of test mock classes, including mock classes related to Jndi,porlet,web applications. In particular, mock classes related to Web applications can greatly improve the convenience of Web Component testing.

Open the Mock folder (path ... mock\org\springframework\mock\web) of spring's download package, and you will find that there are several files:

Mockhttpservletrequest: is a mock implementation of the HttpServletRequest interface, used to simulate the HTTP request of the client, a very common class.

Mockhttpservletresponse: A mock implementation of the HttpServletResponse interface to simulate the server's response to the client.

Mockhttpsession: A mock implementation of the HttpSession interface.

Delegatingservletinputstream: A mock implementation of the ServletInputStream interface.

Delegatingservletoutputstream:servletoutputstream's mock implementation. You can use this class to intercept and analyze the contents of the stream of the server's output.

Others, such as Mockfilterconfig,mockpagecontext (which can test precompiled JSP), Mockrequestdispatcher,mockservletconfig see the name and know it's probably a mock or something.

Give an example:

Mockhttpservletrequest request = new Mockhttpservletrequest

("POST", "/index.do");   

Request.addparameter ("username", "name"); 

request.addparameter ("Password", "word");

Use spring for integration testing:

1, Abstractspringcontexttests class [1], this class all methods are protected, usually do not use this class, and use its subclasses.

2, Abstractdependencyinjectionspringcontexttests class [2]: Inherit from class [1]: name N long. If you only use the Spring Dependency Injection feature, you can have the test case inherit the class.

3, Abstracttransactionalspringcontexttests class [3]: Inherits from class [2], the test cases that inherit the class are performed in spring-managed transactions, and the records on the database do not have any effect after the test is finished. After you have done some work on the database, it will automatically rollback the database, which will ensure that your test has no impact on the environment

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.