我有一个类,该类具有执行循环的方法:
private final ContactServiceConnector contactServiceConnector;
private final ContractServiceConnector contractServiceConnector;
private final PropertyServiceConnector propertyServiceConnector;
private void prepareCrmContactFields(final CrmApiModel crmApiModel, final JsonNode message, final String[] properties) {
try {
JsonNode node = null;
String lastServiceController = "";
for(final String s : properties ) {
final String[] keyVal = s.split(":");
final String actualQbeeProperty = keyVal[3];
switch(keyVal[1]) {
case "user":
case "member":
if( node == null || !"user".equals(lastServiceController) ) {
node = contactServiceConnector.getUserById(userId);
LOGGER.debug("node = " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node));
lastServiceController = keyVal[1];
}
break;
case "building":
if( node == null || !"building".equals(lastServiceController) ) {
node = propertyServiceConnector.getBuildingById(message.get("buildingId").asLong());
LOGGER.debug("node = " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node));
lastServiceController = keyVal[1];
}
break;
default:
lastServiceController = keyVal[1];
crmApiModel.addProperty(new CrmApiModelProperty(keyVal[0],
keyVal[2].isEmpty() ? message.get(keyVal[3]).asText() : node.path(keyVal[2]).get(keyVal[3]).asText()));
continue;
}
crmApiModel.addProperty(new CrmApiModelProperty(keyVal[0],
keyVal[2].isEmpty() ? node.get(keyVal[3]).asText() : node.path(keyVal[2]).get(keyVal[3]).asText()));
}// end for
} catch (InternalCommunicationException e) {
e.printStackTrace();
throw e;
} catch (Exception hceEx) {
throw hceEx;
}
}
}
在我的测试课中,我对此进行了定义:
@MockBean
private ContractServiceConnector contractServiceConnector;
@MockBean
private ContactServiceConnector contactServiceConnector;
@MockBean
private PropertyServiceConnector propertyServiceConnector;
@Test
@Tag("slow")
@Tag("integration")
void publishMessage() throws IOException, InternalCommunicationException, InterruptedException {
Mockito.doReturn(user) .when(contactServiceConnector) .getUserById (Mockito.any(Long.class));
Mockito.doReturn(building).when(propertyServiceConnector).getBuildingById(Mockito.any(Long.class));
Mockito.doReturn(contract).when(contractServiceConnector).getContractById(Mockito.any(Long.class));
...
}
模拟值是这样的:
private JsonNode user = mapper.readTree("{...}");
private JsonNode building = mapper.readTree("{...}");
private JsonNode contract = mapper.readTree("{...}");
具有最初从xyzServiceConnector.getXyzById(id);
返回的值的结构
当我开始测试publishMessage
方法时,有时会使用模拟值设置节点,但通常是null
。我误解了嘲笑还是我在做哪个错误??