Writing actual
Unit test
In the process, in many cases we need to use other components (such as testing with the database). When we use the components provided by third parties, this is not too big, because we can assume that they are not wrong. But once these components are our own components, the problem is exposed.
"The unit test is wrong, but who is wrong. My? His?", This situation clearly violates the independence of the test. This situation has made this test unable to point out that the unit has problems, and it is difficult to make trouble, and it is also a waste of time. At the same time, excessive test code depends on other units, and also acts as some other realistic issues:
- Can't successfully write unit tests before completion of the dependent unit.
- Environmentally dependent, if the test code running an HTTPSERVLETREQUEST processor must start a servlet container.
All of these issues can be solved using Mock Object. The premise to use it is that the interface of the dependent unit must be defined clearly. Easymock is produced for this purpose.
purpose of usage
By simulating the components required by the Unit Test, the purpose of isolating each Unit Test has been reached. The current version 1.1, the environment it needs is JDK1.3.1 or more and JUnit 3.8.1 or more.
You can download it from http://sourceforge.net/projects/easymock/.
usage
1. Easymock uses "Record --- Playback" working mode, basic use steps:
- Create a control object for the MOCK object.
- Get the required MOCK object from the control object.
- Record the methods and return values used in the test method.
- Set the Control object to the "playback" mode.
- carry out testing.
- After the test is completed, confirm that the Mock object has implemented all the operations just defined.
2. Example: Suppose the object you need to test is Requestutil, the method you need is getBoolean. At this point we need to simulate an HTTPSERVLETREQUEST object.
Public void teststringstringhttpservletRequestString () {
Public void testGetBoolean () {
// Controller for creating a MOCK object
MockControl Control = MockControl.createControl (httpservletRequest.class);
/ / Get Mock objects
HTTPSERVLETREQUEST MOCK = (httpservletRequest) Control.getmock ();
/ / Set the method and return value to use in getBoolean
Control.ExpectAndreTurn (Mock.getParameter ("Test"), NULL);
/ / Set the controller to the Replay mode
Control.Replay ();
// Execute test
Assertnull (Requestutil.getstring (Mock, "Test"));
//confirm
Control.verify ();
}
}
With Easymock, perform this test code does not need to start a servlet container, and you can complete it under the way the command line is.
3. Record the behavior of MOCK you need to use. Before using a Mock object, you need to set the method we want to use and the return value of each method. For those methods that are not set, once the call (the controller is in the Replay mode), Easymock will throw an exception. Record a method, usually can be divided into steps: First, if you call this method using the normal object; then, use the controller's setReturnValue function settings. In 1.1, the ExpectandReturn function is provided so that 2 steps can be combined. The main functions are as follows: - EXPECTRETURN, set functions that expect calls, and return values
- EXPECTRHROW, set the function of the desired call, and expect the call to throw an exception
- setReturnValue, set the return value of the last call (as previously called, Request.GetParameter ("Test"), set the return value of Request.getParameter ("Test"))
- setthrowable, set an exception thrown last call
In the Easymock, you can also set the number of times the call performed, please refer to the corresponding Javadoc.
4. Example:
MockControl Control = MockControl.createControl (httpservletRequest.class);
HTTPSERVLETREQUEST MOCK = (httpservletRequest) Control.getmock ();
Mock.getParameter ("Test");
// Set the return value of the request.getParameter for the first time
Control.setReturnValue (NULL, 1);
/ / Set the return value of the second call request.getParameter
Control.setReturnValue ("This Is A Test", 1);
Control.Replay ();
Assertequals (Requestutil.getstring (Mock, "TEST", "Haha"), "Ha");
Assertequals (Requestutil.getstring (Mock, "TEST"), "this is a test");
Control.verify ();
5. After the use of the MOCK object, be sure to call the Verify function of the controller to confirm the Mock object method to get a call.
6. The order of the calling method is used. Sometimes the test code depends on the order of the method that is dependent. If the test code is likely to be in this order when testing the database related code: first open the database link, perform the operation, turn off the link. To better test such code, you can use MockControl.CreateStrictControl () to create a strict Mock object controller, where he specifies the call order of the objects of the MOCK.
The above is the main use of Easymock, as for other usage, see the specific documentation.
checklist
This lists the issues that use the Mock object to make UNIT TEST:
- Do not implement business logic in the Mock object when you do your Mock object.