Test using Mock Object
Author: kongxx
MockObject overview
Testing using Mock Object, mainly to simulate those tools that are not easy to construct in applications (such as HTTPSERVLETREQUEST must be constructed in the servlet container) or complicated objects (such as RESULTSET objects in JDBC) to make test smoothly .
Currently, the main MOCK test tools in Java camps have JMOCK, Mockcreator, Mockrunner, Easymock, Mockmaker, etc., which are mainly nmock, .neetmock, etc. in Microsoft .NET Makers.
The following instructions are used for the common tools for testing the MOCK Object used in Java.
JMOCK
Introduction
Please visit http://www.jmock.org.
ready
1. Get J2SDK;
2. Get JUnit V
3.8.1
(Get a development package from http://www.junit.org);
3. Get JMOCK V1.0 (get JMOCK development packages from http://www.jmock.org).
4. For convenience development, it is recommended to use a good IDE. The code in the following example is debugging in the Eclipse V3.0 environment.
Example
The following is a simple example, the code is as follows:
Package test1;
Import org.jmock. *;
Import javax.servlet.http. *;
Public class mockRequestTest Extends MockObjectTestCase {
Public void testmockRequest1 () {
/ / Construct a MOCK object
Mock Mock = New Mock (httpservletRequest.class);
/ / Set the action to be executed, the following settings indicate that you want to call a httpservletRequest object.
// getParameter method, the passing parameters are "name", and the desired return is "kongxx"
Mock.expects ("" ""). WITH ("Name"). WILL ("kongxx");
/ / Get an HTTPSERVLETREQUEST object according to the MOCK object
HttpservletRequest Request = (httpservletRequest) mock.proxy ();
// assertion result
Assertequals ("Kongxx", Request.getParameter ("name");
}
Public void testmockRequest22 () {
/ / Construct a MOCK object
Mock Mock = New Mock (httpservletRequest.class);
/ / Set the action to be executed, the following settings indicate that you want to call a httpservletRequest object.
// getParameter method, the passing parameters are "name", and the desired return is "kongxx"
Mock.expects ("" ""). WITH ("Name"). WILL ("kongxx");
/ / Get an HTTPSERVLETREQUEST object according to the MOCK object
HttpservletRequest Request = (httpservletRequest) mock.proxy (); // Method for calling HttpServletRequest objects
Request.getParameter ("Name");
// Verify the results
Mock.Verify ();
}
}
Compile and use it as a Test case, it will find that two test methods are successful.
Two examples
Myclass needs to test the class, providing two methods getName and getPassword, all using HTTPSERVLETREQUEST as the input parameters and returns the parameters obtained from HTTPSERVLETREQUEST, the specific code is as follows:
Package test2;
Import javax.servlet.http. *;
public class MyClass {public String getName (HttpServletRequest request) {return request.getParameter ( "name");} public String getPassword (HttpServletRequest request) {return request.getParameter ( "password");}}
The TestmyClass test class, which constructs a fake HttpServletRequest object that tests the getName and getPassword methods in MyClass, the code is as follows:
Package test2;
Import org.jmock. *; import javax.servlet.http. *;
public class TestMyClass extends MockObjectTestCase {private MyClass myclass; private Mock mockRequest; private HttpServletRequest request; public void setUp () {myclass = new MyClass (); // Construct a Mock Object mockRequest = new Mock (HttpServletRequest.class); // The Mock object gets a HttpServletRequest object request = (HttpServletRequest) mockRequest.proxy ();} public void tearDown () {// TODO} public void testGetName () {// set the operation to be performed, the following is provided to call represents a HttpServletRequest object The // getParameter method, the passing parameter is "name", the desired return is "Kongxx" MockRequest.expects ("getParameter"). With (EQ ("name"). Will (ReturnValue)). Will ("kongxx")); assertNotnull (MyClass.getname (Request)); Assertequals ("kongxx", myclass.getname (request)); public void Testget Password () {// Set the action to be executed, the following settings indicate the // getParameter method to call a httpservletRequest object, the password is "password", the desired return is "123456789" mockRequest.eashion ()). Method ("getParameter"). with ("Password")). Will (ReturnValue ("123456789"); assertNotNull (aclass.getpatrol (request)); Assertequals ("123456789", myclass.getpassword (request)) ,}} Compile and use it as a test case, it will find that two test methods have been tested.
summary
no
Easymock
Introduction
Please visit http://www.easymock.org.
ready
1. Get J2SDK;
2. Get JUnit V
3.8.1
(Get a development package from http://www.junit.org);
3. Get Easymock V1.1 (get an Easymock development package from http://www.easymock.org).
4. For convenience development, it is recommended to use a good IDE. The code in the following example is debugging in the Eclipse V3.0 environment.
Example
The following is a simple example, in order to facilitate comparison, the JMOCK example is reached, the code is as follows:
Package test1;
Import org.easymock. *;
Import junit.framework. *;
Import javax.servlet.http. *;
Public Class MockRequestTest Extends Testcase {
Private MockControl Control;
Private httpservletRequest MockRequest;
Public void testmockRequest () {
// Create a MockControl object of Mock HttpservletRequest
Control = MockControl.createControl (httpservletRequest.class);
/ / Get a Mock HttpservletRequest object
MockRequest = (httpservletRequest) control.getmock ();
/ / Set the method of the Mock HttpservletRequest object that is expected to be called
MockRequest.getParameter ("Name");
/ / Set the return value desired by the call method and specify the number of calls
// The following two parameters indicate at least once, up to once
Control.setReturnValue ("KONGXX", 1, 1);
// Set the status of Mock HttpservletRequest,
// indicates that this Mock HttpservletRequest object can be used.
Control.Replay ();
// Use the assertion inspection call
Assertequals ("Kongxx", MockRequest.getParameter ("Name"));
// Verify the desired call
Control.verify ();
}
}
Compile and use it as a Test case, it will find that two test methods are successful.
Two examples
Like an example, an example of Easymock is generated on the JMOCK example.
The class code that needs to be tested is as follows:
Package test2;
Import javax.servlet.http. *;
Public class myclass {
Public string getname (httpservletRequest request) {
Return Request.getParameter ("Name");
}
Public String getPassword (httpservletRequest request) {
Return Request.getParameter ("Password");
}
}
The test class code is as follows:
Package test2;
Import org.easymock. *;
Import junit.framework. *;
Import javax.servlet.http. *;
Public Class TestmyClass Extends Testcase {
Private MockControl Control;
Private httpservletRequest MockRequest;
PRIVATE myclass myclass;
Public void setup () {myclass = new myclass ();
// Create a MockControl object of Mock HttpservletRequest
Control = MockControl.createControl (httpservletRequest.class);
/ / Get a Mock HttpservletRequest object
MockRequest = (httpservletRequest) control.getmock ();
}
Public void teardown () {
// Todo
}
Public void testGetName () {
/ / Set the method of the Mock HttpservletRequest object that is expected to be called
MockRequest.getParameter ("Name");
/ / Set the return value desired by the call method and specify the number of calls
// The following two parameters indicate at least once, up to once
Control.setReturnValue ("Kongxx", 1, 2);
// Set the status of Mock HttpservletRequest,
// indicates that this Mock HttpservletRequest object can be used.
Control.Replay ();
// Use the assertion inspection call
AssertNotnull (MyClass.getName (MockRequest));
Assertequals ("Kongxx", MyClass.getName (MockRequest));
// Verify the desired call
Control.verify ();
}
Public void testgetPassword () {
/ / Set the method of the Mock HttpservletRequest object that is expected to be called
MockRequest.getParameter ("Password");
/ / Set the return value desired by the call method and specify the number of calls
// The following two parameters indicate at least once, up to once
Control.setReturnValue ("KongXX", 1, 2);
// Set the status of Mock HttpservletRequest,
// indicates that this Mock HttpservletRequest object can be used.
Control.Replay ();
// Use the assertion inspection call
AssertNotnull (MyClass.GetPassword);
Assertequals ("kongxx", myclass.getpassword (mockRequest));
// Verify the desired call
Control.verify ();
}
}
Compile and use it as a Test case, it will find that two test methods are successful.
summary
no
to sum up
no
Reference
l http://www.mockObjects.com/ introduced the basic concepts of key Mock Object and the main MOCK test tools currently in each environment.
l JMOCK Home http://www.jmock.org, you can get JMOCK's latest code and development packs, and some explanation documents.
l Easymock's Home http://www.easymock.org, you can get the latest code and development packs of JMOCK, and some explanation documents.
l Nmock's homepage http://www.nmock.org, which introduces a development tool for MOCK testing on the Microsoft .NET platform.
l Mockcreator Home http://mockcreat.sourceforge.net/.