标签:
现有两个类如下:
SimpleTool.java:
public class SimpleTool {
public String fun1(String str) {
return "real: public String fun1(" + str + ")";
}
private String fun2(String str) {
return "real: private String fun2(" + str + ")";
}
public String fun3(String str) {
return "real: public String fun3(" + str + ")";
}
public String fun4(String str) {
return fun2(str);
}
}
UseSimpleTool.java:
public class UseSimpleTool {
public String fun1(String name) {
SimpleTool simpleTool = new SimpleTool();
return simpleTool.fun1(name);
}
}
@Mocked
SimpleTool simpleTool;
@Test
public void testExpectation() {
//未mock函数返回null
new Expectations() {
{
simpleTool.fun1(anyString);
result = "MOCK";
}
};
System.out.println(simpleTool.fun1("param"));
System.out.println(simpleTool.fun3("param"));
System.out.println(new UseSimpleTool().fun1("param"));
new Verifications() {
{
simpleTool.fun1(anyString);
times = 2;
}
};
}
输出:
MOCK
null
MOCK
@Injectable
SimpleTool simpleTool;
@Test
public void testExpectation() {
//未mock函数返回null
new Expectations() {
{
simpleTool.fun1(anyString);
result = "MOCK";
}
};
System.out.println(simpleTool.fun1("param"));
System.out.println(simpleTool.fun3("param"));
System.out.println(new UseSimpleTool().fun1("param"));
new Verifications() {
{
simpleTool.fun1(anyString);
times = 1;
}
};
}
输出:
MOCK
null
real: public String fun1(param)
@Mocked
SimpleTool simpleTool;
@Test
public void testExpectation() {
new Expectations() {
{
simpleTool.fun1(anyString);
result = new Delegate<String>() {
public String aDelegateMethod(String str) {
return str.equals("param0") ? "MOCK0" : "MOCK1";
}
};
}
};
System.out.println(simpleTool.fun1("param0"));
System.out.println(simpleTool.fun3("param"));
System.out.println(new UseSimpleTool().fun1("param1"));
new Verifications() {
{
simpleTool.fun1(anyString);
times = 2;
}
};
}
输出:
MOCK0
null
MOCK1
@Test
public void testMockUp() {
//影响该类所有实例
new MockUp<SimpleTool>(){
//未mock函数不受影响
@Mock
public String fun1(String str) {
return "(MOCK)";
}
};
SimpleTool simpleTool = new SimpleTool();
System.out.println(simpleTool.fun1("param"));
System.out.println(simpleTool.fun3("param"));
UseSimpleTool useSimpleTool = new UseSimpleTool();
System.out.println(useSimpleTool.fun1("param"));
}
输出:
(MOCK)
real: public String fun3(param)
(MOCK)
@Test
public void testMockUp() {
new MockUp<SimpleTool>(){
//未mock函数不受影响
@Mock
public String fun2(String str) {
return "(MOCK)";
}
};
SimpleTool simpleTool = new SimpleTool();
System.out.println(simpleTool.fun1("param"));
System.out.println(simpleTool.fun3("param"));
System.out.println(simpleTool.fun4("param"));
}
输出:
real: public String fun1(param)
real: public String fun3(param)
(MOCK)
现有接口及其实现类如下:
SimleInterface.java:
public interface SimpleInterface {
String getCityName();
String getAreaName();
}
SimpleInterfaceImpl.java:
public class SimpleInterfaceImpl implements SimpleInterface {
public String getCityName() {
return "default city";
}
public String getAreaName() {
return "default area";
}
}
实例:
//此处使用Injectable和Mocked为一样的效果
@Injectable
SimpleInterface simpleInterface;
@Test
public void testUpper() throws Exception {
SimpleInterface mockInstance = new MockUp<SimpleInterface>() {
@Mock
public String getCityName() {
return "BEIJING(MOCK)";
}
@Mock
public String getAreaName() {
return "HUABEI(MOCK)";
}
}.getMockInstance();
System.out.println(mockInstance.getCityName());
System.out.println(mockInstance.getAreaName());
System.out.println(simpleInterface.getCityName());
System.out.println(simpleInterface.getAreaName());
SimpleInterfaceImpl simpleInterfaceImpl = new SimpleInterfaceImpl();
System.out.println(simpleInterfaceImpl.getCityName());
System.out.println(simpleInterfaceImpl.getAreaName());
}
输出:
BEIJING(MOCK)
HUABEI(MOCK)
null
null
default city
default area
对于接口的Mock如何影响实现该接口的所有类的对象,尚需进一步研究。
标签:
原文地址:http://blog.csdn.net/foreverling/article/details/51234149