gpt4 book ai didi

java - Mockito 验证单元测试 - 需要但未调用。事实上,与这个模拟的互动为零

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:16 25 4
gpt4 key购买 nike

首先我想为我的英语感到抱歉。

我开始做一些单元测试(我以前从未这样做过,我是编程新手)。

我必须使用mockito.verify测试将产品添加到数据库(DynamoDB)的简单方法,但我有

"Wanted but not invoked. Actually, there were zero interactions with this mock." 

错误,我不知道该怎么办。

这是我的方法代码(在 KitchenService 类中):

public Product addProduct(Product content) {

ObjectMapper objectMapper = new ObjectMapper();

String mediaJSON = null;
String authorJSON = null;
String productKindsJSON = null;
try {
mediaJSON = objectMapper.writeValueAsString(content.getMedia());
authorJSON = objectMapper.writeValueAsString(content.getAuthor());
productKindsJSON = objectMapper.writeValueAsString(content.getProductKinds());
} catch (JsonProcessingException e) {
logger.log(e.getMessage());
}


Item item = new Item()
.withPrimaryKey("id", UUID.randomUUID().toString())
.with("name", content.getName())
.with("calories", content.getCalories())
.with("fat", content.getFat())
.with("carbo", content.getCarbo())
.with("protein", content.getProtein())
.with("productKinds", productKindsJSON)
.with("author", authorJSON)
.with("media", mediaJSON)
.with("approved", content.getApproved());


Item save = databaseController.saveProduct(PRODUCT_TABLE, item);
logger.log(save + " created");



return content;

}

这是测试代码:

@Test
public void addProduct() throws Exception {


KitchenService instance = mock(KitchenService.class);


Product expectedProduct = new Product();
expectedProduct.setName("kaszanka");
expectedProduct.setCalories(1000);
expectedProduct.setFat(40.00);
expectedProduct.setCarbo(20.00);
expectedProduct.setProtein(40.00);
expectedProduct.setProductKinds(Collections.singletonList(ProductKind.MEAT));
expectedProduct.setApproved(false);
Author expectedAuthor = new Author();
expectedAuthor.setId("testID");
expectedAuthor.setName("Endrju Golota");
expectedProduct.setAuthor(expectedAuthor);
Media expectedMedia = new Media();
expectedMedia.setMediaType(MediaType.IMAGE);
expectedMedia.setName("dupajasia");
expectedMedia.setUrl("http://blabla.pl");
expectedProduct.setMedia(expectedMedia);

verify(instance, times(1)).addProduct(expectedProduct);
}

这是我测试后得到的:

Wanted but not invoked:
kitchenService.addProduct(
model.kitchen.Product@a0136253
);
-> at service.kitchen.KitchenServiceTest.addProduct(KitchenServiceTest.java:80)
Actually, there were zero interactions with this mock.

有人可以告诉我我做错了什么吗?

最佳答案

您应该模拟和验证的是 databaseController 依赖项:

@Test
public void addProduct() throws Exception {

KitchenService instance = new KitchenService(); // you should create the class under test

DatabaseController controllerMock = mock(DatabaseController.class); // mock the controller

instance.setController(controller); // inject the mock

...

// Act
instance.addProduct(expectedProduct);

// Assert
verify(controller).saveProduct(Mockito.eq(PRODUCT_TABLE), Mockito.any(Item.class));

}

您应该验证数据库是否在服务内被调用。检查是否使用任何 Item 对象调用数据库就足够了。

关于java - Mockito 验证单元测试 - 需要但未调用。事实上,与这个模拟的互动为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com