是否可以测试serviceA的方法,并告诉测试环境用mockServiceB替换从serviceA到serviceB的所有调用?
这是我想测试的方法(ServiceA.java
):
@Inject
ServiceB serviceB;
public boolean tokenExists(ItemModel item) throws Exception {
try {
List<ItemModel > items = serviceB.getItems();
String token = item.getToken();
return items.stream().filter(i -> i.getToken().equals(token)).findFirst().isPresent();
} catch (ApiException e) {
throw new Exception(500, "Data could not be fetched.", e);
}
}
由于serviceB.getItems()
将导致REST调用,因此我想将对serviceB
的调用替换为自定义的mockServiceB
,其中我只是从json文件加载模拟数据,如下所示(TestServiceA.java
):
@InjectMock
ServiceB serviceB;
@ApplicationScoped
public static class ServiceB {
private ObjectMapper objMapper = new ObjectMapper();;
public List<ItemModel> getItems() throws IOException {
final String itemsAsJson;
try {
itemsAsJson= IOUtils.toString(new FileReader(RESOURCE_PATH + "items.json"));
objMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<Item> items= objMapper.readValue(itemsAsJson, objMapper.getTypeFactory().constructCollectionType(List.class, ItemModel.class));
return items;
} catch (IOException e) {
throw e;
}
}
}
然后测试tokenExists
方法,如下所示(TestServiceA.java
):
@Test
public void testTokenExists() {
try {
Assertions.assertTrue(this.serviceA.tokenExists(this.getTestItem()));
} catch (Exception e) {
fail("exception");
}
}
然而,当我运行测试testTokenExists
时,它仍然调用原来的serviceB.getItems()
。
1条答案
按热度按时间kd3sttzy1#
你需要在
ServiceA
类里面模拟serviceB
,你有两种不同的选择:首先您可以使用
whitebox
或reflection
,并将模拟的serviceB
分配给ServiceA
类第二个
您可以使用
@InjectMocks
注意:也准备好你的跑步者