[Transfer] Mock testing: in the testing process, a virtual object is used to create a testing method for objects that are not easily constructed or retrieved. Mock object: this virtual object is a mock object. A mock object is a substitute for a real object during debugging. Mock object usage scope:
- Real objects have uncertain behaviors and produce unpredictable results (such as stock quotations and weather forecasts)
- Difficult to create real objects
- Some actions of real objects are difficult to trigger.
- Real objects do not actually exist (dealing with other development groups or new hardware), etc...
Key steps for testing with mock objects:
- Use an interface to describe this object
- Implement this interface in the product code
- Implement this interface in the test code
- In the tested code, only objects are referenced through interfaces. Therefore, it does not know whether the referenced object is a real object or a mock object.
Mockobject Overview Testing with mock object is mainly used to simulate structures that are not easy to construct in the application (for example, httpservletrequest must be constructed in the servlet container) or more complex objects (such as the resultset object in JDBC) to make the test run smoothly. Currently, the main mock testing tools in the Java camp include jmock, mockcreator, mockrunner, easymock, and mockmaker. in Microsoft's. Net camp, they are mainly nmock and. netmock. The following describes the common tools used to test mock objects in Java. Jmock Introduction Preparation 1. Obtain j2sdk; 2. Get JUnit v3.8.1 (get the development kit from http://www.junit.org ); 3. Get jmock V1.0 (get jmock development kit from http://www.jmock.org ). 4. For ease of development, we recommend that you use a good ide. The Code in the following example is all debugged in the eclipse V3.0 environment. Example 1 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 operation to be executed. The following settings indicate that you want to call // Getparameter method. The parameter passed is "name" and the expected return is "kongxx" Mock. Expects (once (). Method ("getparameter"). With (eq ("name"). Will (returnvalue ("kongxx ")); // Obtain an httpservletrequest object based on the mock object Httpservletrequest request = (httpservletrequest) Mock. Proxy (); // Asserted call result Assertequals ("kongxx", request. getparameter ("name ")); } Public void testmockrequest2 (){ // Construct a mock object Mock Mock = new mock (httpservletrequest. Class ); // Set the operation to be executed. The following settings indicate that you want to call // Getparameter method. The parameter passed is "name" and the expected return is "kongxx" Mock. Expects (once (). Method ("getparameter"). With (eq ("name"). Will (returnvalue ("kongxx ")); // Obtain an httpservletrequest object based on the mock object Httpservletrequest request = (httpservletrequest) Mock. Proxy (); // Call the httpservletrequest object Method Request. getparameter ("name "); // Verify the execution result Mock. Verify (); } } Compile and run it as a test case. Both test methods are successfully tested. Example 2 The class to be tested by myclass provides two methods: getname and GetPassword. Both methods use httpservletrequest as the input parameter and return the parameters obtained from httpservletrequest. The 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 constructs a false httpservletrequest object to test 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; Private mock mockrequest; Private httpservletrequest request; Public void setup (){ Myclass = new myclass (); // Construct a mock object Mockrequest = new mock (httpservletrequest. Class ); // Obtain an httpservletrequest object based on the mock object Request = (httpservletrequest) mockrequest. Proxy (); } Public void teardown (){ // Todo } Public void testgetname (){ // Set the operation to be executed. The following settings indicate that you want to call // Getparameter method. The parameter passed is "name" and the expected return is "kongxx" Mockrequest. Expects (atleastonce (). Method ("getparameter"). With (eq ("name"). Will (returnvalue ("kongxx ")); Assertnotnull (myclass. getname (request )); Assertequals ("kongxx", myclass. getname (request )); } Public void testgetpassword (){ // Set the operation to be executed. The following settings indicate that you want to call // Getparameter method. The parameter passed is "password" and the expected return value is "123456789" Mockrequest. Expects (atleastonce (). Method ("getparameter"). With (eq ("password"). Will (returnvalue ("123456789 ")); Assertnotnull (myclass. GetPassword (request )); Assertequals ("123456789", myclass. GetPassword (request )); } } Compile and run it as a test case. Both test methods are successfully tested. Easymock Introduction Preparation 1. Obtain j2sdk; 2. Get JUnit v3.8.1 (get the development kit from http://www.junit.org ); 3. Get easymock V1.1 (get easymock from http://www.easymock.org ). 4. For ease of development, we recommend that you use a good ide. The Code in the following example is all debugged in the eclipse V3.0 environment. Example 1 Here is a simple example. To facilitate the comparison, we re-implement jmock Example 1. 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 for mock httpservletrequest Control = mockcontrol. createcontrol (httpservletrequest. Class ); // Obtain a mock httpservletrequest object Mockrequest = (httpservletrequest) control. getmock (); // Set the mock httpservletrequest object to be called Mockrequest. getparameter ("name "); // Set the expected return value of the call method and specify the number of calls // The following two parameters indicate at least one call and at most one call. Control. setreturnvalue ("kongxx", 1, 1 ); // Set the status of mock httpservletrequest, // Indicates that the mock httpservletrequest object can be used. Control. Replay (); // Use assertion to check the call Assertequals ("kongxx", mockrequest. getparameter ("name ")); // Verify the expected call Control. Verify (); } } Compile and run it as a test case. Both test methods are successfully tested. Example 2 As in the example, easymock Example 2 is generated for the two new implementations of the jmock example. The code of the class 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 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; Public void setup (){ Myclass = new myclass (); // Create a mockcontrol object for mock httpservletrequest Control = mockcontrol. createcontrol (httpservletrequest. Class ); // Obtain a mock httpservletrequest object Mockrequest = (httpservletrequest) control. g
|