public static mockcontrol createxxxcontrol(class clazz);
public static mockcontrol createxxxcontrol(class clazz,
class[] argtypes, object[] args);
classwithnodefaultconstructor是一个没有缺省构造函数的类:
public class classwithnodefaultconstructor {
public classwithnodefaultconstructor(int i) {
...
}
...
}
//get mock control
mockcontrol control = mockcontrol.createcontrol(foo.class);
//get the mock object from mock control
foo foo = (foo) control.getmock();
//foo.java
public class foo {
public void dummy() throw parseexception {
...
}
public string bar(int i) {
...
}
public boolean issame(string[] strs) {
...
}
public void add(stringbuffer sb, string s) {
...
}
}
//get mock control
mockcontrol control = mockcontrol.createcontrol(foo.class);
//get mock object
foo foo = (foo)control.getmock();
//begin behavior definition
//specify which method invocation's behavior
//to be defined.
foo.bar(10);
//define the behavior -- return "ok" when the
//argument is 10
control.setreturnvalue("ok");
...
//end behavior definition
control.replay();
...
mockcontrol control = ...
foo foo = (foo)control.getmock();
...
foo.bar(10);
//define the behavior -- return "ok" when the
//argument is 10. and this method is expected
//to be called just once.
setreturnvalue("ok", 1);
...
...
foo.bar(10);
//define the behavior -- return "ok" when the
//argument is 10. and this method is expected
//to be called at least once and at most 3
//times.
setreturnvalue("ok", 1, 3);
...
...
foo.bar(10);
//define the behavior -- return "ok" when the
//argument is 10. and this method is expected
//to be called at least once.
setreturnvalue("ok", range.one_or_more);
...
...
foo.bar(10);
//define the behavior -- return "ok" when calling
//bar(int) despite the argument value.
setdefaultreturnvalue("ok");
...
...
try {
foo.dummy();
} catch (exception e) {
//skip
}
//define the behavior -- throw parseexception
//when call dummy(). and this method is expected
//to be called exactly once.
control.setthrowable(new parseexception("", 0), 1);
...
...
try {
foo.dummy();
} catch (exception e) {
//skip
}
//define the behavior -- no return value
//when calling dummy(). and this method is expected
//to be called at least once.
control.setvoidcallable();
...
public interface argumentsmatcher {
public boolean matches(object[] expected,
object[] actual);
}...
foo.issame(null);
//set the argument match rule -- always match
//no matter what parameter is given
control.setmatcher(mockcontrol.always_matcher);
//define the behavior -- return true when call
//issame(). and this method is expected
//to be called at least once.
control.setreturnvalue(true, 1);
...
...
//just to demonstrate the function
//of out parameter value definition
foo.add(new string[]{null, null});
//set the argument match rule -- always
//match no matter what parameter given.
//also defined the value of out param.
control.setmatcher(new argumentsmatcher() {
public boolean matches(object[] expected,
object[] actual) {
((stringbuffer)actual[0])
.append(actual[1]);
return true;
}
});
//define the behavior of add().
//this method is expected to be called at
//least once.
control.setvoidcallable(true, 1);
...
//get mock control
mockcontrol control = ...;
//get mock object
foo foo = (foo)control.getmock();
//set default argumentsmatcher
control.setdefaultmatcher(
mockcontrol.always_matcher);
//begin behavior definition
foo.bar(10);
control.setreturnvalue("ok");
...
package org.jingle.mocquer.sample;
import java.io.ioexception;
import java.net.socketexception;
import org.apache.commons.net.ftp.ftpclient;
public class ftpconnector {
//ftp server host name
string hostname;
//ftp server port number
int port;
//user name
string user;
//password
string pass;
public ftpconnector(string hostname,
int port,
string user,
string pass) {
this.hostname = hostname;
this.port = port;
this.user = user;
this.pass = pass;
}
/**
* connect to the ftp server.
* the max retry times is 3.
* @return true if succeed
*/
public boolean connect() {
boolean ret = false;
ftpclient ftp = getftpclient();
int times = 1;
while ((times <= 3) && !ret) {
try {
ftp.connect(hostname, port);
ret = ftp.login(user, pass);
} catch (socketexception e) {
} catch (ioexception e) {
} finally {
times++;
}
}
return ret;
}
/**
* get the ftpclient instance
* it seems that this method is a nonsense
* at first glance. actually, this method
* is very important for unit test using
* mock technology.
* @return ftpclient instance
*/
protected ftpclient getftpclient() {
return new ftpclient();
}
}
package org.jingle.mocquer.sample;
import java.io.ioexception;
import org.apache.commons.net.ftp.ftpclient;
import org.jingle.mocquer.mockcontrol;
import junit.framework.testcase;
public class ftpconnectortest extends testcase {
/*
* @see testcase#setup()
*/
protected void setup() throws exception {
super.setup();
}
/*
* @see testcase#teardown()
*/
protected void teardown() throws exception {
super.teardown();
}
/**
* test ftpconnector.connect()
*/
public final void testconnect() {
//get strict mock control
mockcontrol control =
mockcontrol.createstrictcontrol(
ftpclient.class);
//get mock object
//why final? try to remove it
final ftpclient ftp =
(ftpclient)control.getmock();
//test point 1
//begin behavior definition
try {
//specify the method invocation
ftp.connect("202.96.69.8", 7010);
//specify the behavior
//throw ioexception when call
//connect() with parameters
//"202.96.69.8" and 7010. this method
//should be called exactly three times
control.setthrowable(
new ioexception(), 3);
//change to working state
control.replay();
} catch (exception e) {
fail("unexpected exception: " + e);
}
//prepare the instance
//the overridden method is the bridge to
//introduce the mock object.
ftpconnector inst = new ftpconnector(
"202.96.69.8",
7010,
"user",
"pass") {
protected ftpclient getftpclient() {
//do you understand why declare
//the ftp variable as final now?
return ftp;
}
};
//in this case, the connect() should
//return false
assertfalse(inst.connect());
//change to checking state
control.verify();
//test point 2
try {
//return to preparing state first
control.reset();
//behavior definition
ftp.connect("202.96.69.8", 7010);
control.setthrowable(
new ioexception(), 2);
ftp.connect("202.96.69.8", 7010);
control.setvoidcallable(1);
ftp.login("user", "pass");
control.setreturnvalue(true, 1);
control.replay();
} catch (exception e) {
fail("unexpected exception: " + e);
}
//in this case, the connect() should
//return true
asserttrue(inst.connect());
//verify again
control.verify();
}
}
try {
ftp.connect("202.96.69.8", 7010);
control.setthrowable(new ioexception(), 3);
control.replay();
} catch (exception e) {
fail("unexpected exception: " + e);
}ftpconnector inst = new ftpconnector("202.96.69.8",
7010,
"user",
"pass") {
protected ftpclient getftpclient() {
return ftp;
}
};
assertfalse(inst.connect());
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器