Spring Learning 12-spring Unit Testing with mock

Source: Internet
Author: User
Tags connection pooling

I. Overview
For Java component developers, they are looking for a set of easy-to-use unit tests that provide comprehensive testing capabilities for component development. Testing traditional J2eeweb components has always been a more difficult task than testing a standalone Java object, because Web components must be running on some server platform and they are also associated with HTTP-based web interaction details.
Testability (testing each component in the framework, regardless of its specific type) is one of the key principles advocated by the spring framework. From this perspective, spring is a significant improvement on the core Java EE model-in the past, component testing outside the container is difficult to implement, and even in-container testing often requires a complex installation process.
Mock test: A test method that is created to test for some objects that are not easily constructed or are not easily accessible during the test process, using a dummy object. This virtual object is a mock object. Mock objects are substitutes for real objects during commissioning.
Brief introduction of Spring mock class
The mock object is a term that is primarily popular in extreme programmers and junit groups. In the context of a unit test, a mock object refers to an object that can "emulate" some object interfaces with some "fictional placeholder" functionality. During testing, these fictional placeholder objects can be used in a simple way to mimic the desired behavior and results for a component, allowing you to focus on the component itself thoroughly and without worrying about other dependency issues.

Spring provides a mock implementation for each of the key interfaces from the Web side of the Java EE:

Mockhttpservletrequest
-This class is used in almost every unit test and is a mock implementation of the most common interface HttpServletRequest for j2eeweb applications.

Mockhttpservletresponse-This object is used for the mock implementation of the HttpServletResponse interface.

mockhttpsession-This is another frequently used mock object (this class will be discussed later in this article for the application of Session binding processing).

Delegatingservletinputstream
-This object is used for the mock implementation of the ServletInputStream interface.

Delegatingservletoutputstream-This object will implement the proxy servletoutputstream. You can use it when you need to intercept and analyze the content written to an output stream.

In summary, these objects are most useful when implementing your own test controller. However, Spring also provides the following mock implementations corresponding to other less commonly used components (if you are a low-level API developer, you might find their own corresponding usage):

Mockexpressionevaluator-This mock object is used primarily when you want to develop and test your own JSTL-based tag library.

Mockfilterconfig-This is a mock implementation of the Filterconfig interface.

Mockpagecontext-This is a mock implementation of the Jsppagecontext interface. You will find that the use of this object facilitates the testing of precompiled JSPs.

MockrequestdispatcherA mock implementation of the-requestdispatcher interface, which you use primarily within other mock objects.

Mockservletconfig-This is a mock implementation of the ServletConfig interface. When a unit tests a Web component, such as a Web component provided by the Struts framework, you are asked to set theMockservletcontext-The ServletConfig and ServletContext interfaces implemented.

So, how do we use these mock objects? We know that HttpServletRequest is a component that holds a fixed value that describes the HTTP parameters, and it is these parameters that drive the functionality of the Web component. Mockhttpservletrequest, as an implementation of the HttpServletRequest interface, allows you to set these immutable parameters. In a typical Web Component test scenario, you can instantiate the object and set any of these parameters as follows:
//Specify form methods and form behavior
Mockhttpservletrequest request = new Mockhttpservletrequest ("GET", "/main.app");

Request.addparameter ("Choice", expanded);
Request.addparameter ("ContextMenu", "left");

Similarly, you can instantiate and comprehensively control and analyze HttpResponse and HttpSession objects. Next, let's briefly look at the specific JUnit framework extensions provided by spring.
Database connection Pooling Technology

Spring Learning 12-spring Unit Testing with mock

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.