Testing with mock object

Source: Internet
Author: User

Use Mock Object to test

Author: kongxx Mockobject Overview

Using mock object for testing, It is primarily used to simulate tools that are not easily constructed in an application (such as HttpServletRequest must be constructed in a servlet container) or more complex objects (such as the ResultSet object in JDBC) to make the test work smoothly.

Currently, the main mock test tools in the Java camp are Jmock,mockcreator,mockrunner,easymock,mockmaker, etc., at Microsoft. NET camp is mainly NMock,. Netmock and so on.

Here's how to use the common tools for testing mock object in Java. JMock Brief Introduction

Please visit http://www.jmock.org. Ready to

1. access to J2SDK;

2. Get junit v<?xml:namespace prefix = st1 ns = "Urn:schemas-microsoft-com:office:smarttags"/>3.8.1 (from HTTP://WW W.junit.org to obtain the development package);

3. Get Jmock V1.0 (obtain Jmock development package from http://www.jmock.org).

4. For easy development, it is recommended to use a good IDE. The code in the following example is debugging in the Eclipse V3.0 environment. Example One

Here's a simple example of the following code:

Package Test1;<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Import org.jmock.*;

Import javax.servlet.http.*;

public class Mockrequesttest extends mockobjecttestcase{

public void TestMockRequest1 () {

Construct a mock object

Mock mocks = new mocks (httpservletrequest.class);

Set the action to be performed, the following setting represents the HttpServletRequest object to invoke the

GetParameter method, the passed parameter is "name" and the desired return is "kongxx"

Mock.expects (Once ()). Method ("GetParameter"). With (eq ("name")). would (ReturnValue ("kongxx"));

Get a HttpServletRequest object from a mock object

HttpServletRequest request = (HttpServletRequest) mock.proxy ();

Assertion Call Result

Assertequals ("Kongxx", Request.getparameter ("name"));

}

public void TestMockRequest2 () {

Construct a mock object

Mock mocks = new mocks (httpservletrequest.class);

Set the action to be performed, the following setting represents the HttpServletRequest object to invoke the

GetParameter method, the passed parameter is "name" and the desired return is "kongxx"

Mock.expects (Once ()). Method ("GetParameter"). With (eq ("name")). would (ReturnValue ("kongxx"));

Get a HttpServletRequest object from a mock object

HttpServletRequest request = (HttpServletRequest) mock.proxy ();

method to invoke the HttpServletRequest object

Request.getparameter ("name");

Validating execution Results

Mock.verify ();

}

}

compiled and run as a test case, you will find that two test methods are tested successfully. Example Two

MyClass needs to test the class, provides two methods GetName and GetPassword, all use httpservletrequest as input parameters and return the parameters obtained from the 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, in which a fake HttpServletRequest object is constructed to test the GetName and GetPassword methods in MyClass, the code reads 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);

Get a HttpServletRequest object from a mock object

Request = (HttpServletRequest) mockrequest.proxy ();

}

public void teardown () {

Todo

}

public void Testgetname () {

Set the action to be performed, the following setting represents the HttpServletRequest object to invoke the

GetParameter method, the passed parameter is "name" and the desired return is "kongxx"

Mockrequest.expects (Atleastonce ()). Method ("GetParameter"). With (eq ("name")). would (ReturnValue ("kongxx"));

Assertnotnull (Myclass.getname (request));

Assertequals ("Kongxx", Myclass.getname (Request));

}

public void Testgetpassword () {

Set the action to be performed, the following setting represents the HttpServletRequest object to invoke the

GetParameter method, the passed parameter is "password" and the desired return is "123456789"

Mockrequest.expects (Atleastonce ()). Method ("GetParameter"). With (eq ("password")). would (ReturnValue ("123456789"));

Assertnotnull (Myclass.getpassword (request));

Assertequals ("123456789", Myclass.getpassword (Request));

}

}

Compiled and run as a test case, you will find that two test methods are tested successfully. Summary

No EasyMock Introduction

Please visit http://www.easymock.org. Ready to

1. access to J2SDK;

2. Obtain JUnit V3.8.1 (from http://www.junit.org to get development packages);

3. Get Easymock V1.1 (obtain Easymock development package from http://www.easymock.org).

4. For easy development, it is recommended to use a good IDE. The code in the following example is debugging in the Eclipse V3.0 environment. Example One

Here is a simple example, in order to facilitate the comparison, here Jmock example one, the code is as follows:

Package test1;

 

Import org.easymock.*;

Import junit.framework.*;

Import javax.servlet.http.*;

 

Public class Mockrequesttest extends testcase{

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.