Use JMOCK auxiliary unit testing

zhaozj2021-02-11  253

Use JMOCK auxiliary unit testing

Author: ice clouds Time: 2004-04-16 Contact: icecloud (AT) sina.comBlog: http: //icecloud.51.net

JMOCK is an enhanced library for Junit. Source from http://www.jmock.org.

JMOCK is easy to use, very suitable for MOCK test mode in TDD

First let's see a simple test code.

package name.nona.test.jmock; import java.sql.SQLException; import java.sql.Statement; import org.jmock.Mock; import org.jmock.MockObjectTestCase; public class TestUsageOfJmock extends MockObjectTestCase {public void testDatabaseUseage () throws SQLException {Mock MockStmt = New Mock (statement.class); String SQL = "Select * from test"; mockstmt.expect (onCE ()) .Method ("Execute") .with (EQ (SQL)). Will (ReturnValue False); statement stmt = (statement) mockstmt.proxy (); assertfalse (stmt.execute (sql));}}

It can be seen that the above is a STATEMENT object that makes a fake (MOCK).

Returns a FALSE value by an Execute operation of this object.

Where the key statement is

MockStmt.expect ("Execute"). With (EQ (SQL)). Will (ReturnValue (false);

Explain as follows:

EXPECT: The number of expected executions can be onCe, atleastonce (), notcalled ().

Method is expected to call the method name, here is Execute

The parameters required by the with method, if not, don't write with it, directly WILL

Will returns a value, no don't write.

The ONCE, EQ, RETURNVALUE is a method inheriting from the MockObjectTestCase.

After execution, Green, success. Try twice to call AssertFalse (Stmt.execute (SQL)); prompt error.

JMOCK is the MOCK feature implemented by DynamicProxy through CGLIB.

Note: This article is only a brief introduction to JMOCK, without much content. More things need you to explore, such as JMOCK design is very interesting, using ValueObject mode, etc. If you are interested in JMOCK, please visit http://www.jmock.org. You can download the latest JMOCK package, which may be different on the interface, but the basic call has been constant (as above).

1.0STable version is under development, helps very much, you can learn the latest usage by watching Javadoc and Testcase.

转载请注明原文地址:https://www.9cbs.com/read-4941.html

New Post(0)