我的问题是如何使用Mockito的doAnswer调用原始方法并使用我自己的测试参数。更复杂的是,其中一个参数不是原始的。这是一个自定义类。
鉴于以下内容:
public class EnvironmentQualityStatus {
public int environmentQuality = EnvironmentQualityStatus.NA;
public long cht = -1;
public int environmentSubType = -1;
public String engineer="";
public int engineerType = -1;
public long timeStamp = 0;
public static final String version = "tpv1";
EnvironmentQualityStatus(){}
EnvironmentQualityStatus(int environmentQuality, long cht, int environmentSubType, String engineer, int engineerType) {
this.environmentQuality = environmentQuality;
this.cht = cht;
this.environmentSubType = environmentSubType;
this.engineer = engineer;
this.engineerType = engineerType;
timeStamp = System.currentTimeMillis();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("ts: ");
sb.append(timeStamp);
sb.append(", quality: ");
sb.append(EnvironmentQualityStatus.valueOf(environmentQuality));
sb.append(", cht: ");
sb.append(cht);
sb.append(", type: ");
if (engineerType == Engineers.TYPE_CIVIL) {
sb.append(“civil”);
} else if (engineerType == Engineers.TYPE_INDUSTRIAL) {
sb.append(“industrial”);
if (!TextUtils.isEmpty(engineer)) {
sb.append(", Engineer: ");
sb.append(engineer);
}
} else {
sb.append("n/a");
}
return sb.toString();
}
}
使用上面的代码在类中设置一些东西。正是这个电话(以及其他类似的电话)我想用Mockito嘲笑。
public class EnvironmentQuality {
...
public void updateEnvironmentQuality(EnvironmentQualityStatus eqStatus ,
int environmentQuality ,int cht , String engineer, boolean notify ){
//set some stuff....
}
}
据我所知,使用Mockito的doAnswer是正确的方法。我只是无法弄清楚如何连接最后一部分,我用自己操作调用值,以便在测试运行时使用它们的程序获取我的值。我已经阅读了Mockito文档以及围绕这个主题的几个SO帖子,但我仍然不清楚。
EnvironmentQuality environmentQuality = spy((EnvironmentQuality)EnvironmentQuality.getInstance(context));
spy(environmentQuality).updateNetworkQuality(Mockito.any(com.something.internal.EnvironmentQualityStatus.class), anyInt(), anyInt(), anyString(), anyBoolean());
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
//This is an example of how to spy the parameters that were passed.
EnvironmentQualityStatus status = (EnvironmentQualityStatus)invocation.getArguments()[0];
int environmentQuality = (int)invocation.getArguments()[1];
int cht = (int)invocation.getArguments()[2];
String engineer = (String)invocation.getArguments()[3];
boolean notify = (boolean)invocation.getArguments()[4];
//But how to customize the parameters so that every time updateEnvironmentQuality(...) is called, a test can customize the params and dynamically influence the values.
return null;
}
}).when(environmentQuality).updateEnvironmentQuality(Mockito.any(com.something.internal.EnvironmentQualityStatus.class), anyInt(), anyInt(), anyString(), anyBoolean());
答案 0 :(得分:1)
所以我假设你想用其他值调用该方法。通过查看InvocationOnMock
的{{3}},您可以查看如何获取被调用的Method
和Object
。所以这应该很容易修改:
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object callingObject = invocation.getMock();
Method method = invocation.getMethod();
method.invoke(callingObject, /* arguments... */);
return null;
}