My Question is how to use Mockito's doAnswer to call the original method and with my own test parameters. To complicate matters, one of the parameters is not a primitive. It's a custom class.
Given the following:
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();
}
}
Taking into use the code above to set some stuff in a class. It's this call (and others like this) that I'd like to mock using Mockito.
public class EnvironmentQuality {
...
public void updateEnvironmentQuality(EnvironmentQualityStatus eqStatus ,
int environmentQuality ,int cht , String engineer, boolean notify ){
//set some stuff....
}
}
I understand that using Mockito's doAnswer is the right way to do this. I just can't figure out how to wire up the very last part, where I manipulate the called values with my own so that the program that uses them at test-run-time gets my values. I've read Mockito docs as well as several SO posts around this topic, but it's still not clear to me.
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());