java Mockito:模拟对象并添加到 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37272250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito: mocking objects and adding to ArrayList
提问by shirafuno
I am testing an enterprise level application using Mockito and JUnit. Here is the code for a method of adding a product to the offline repository class in a product offline-repository-class-test I have:
我正在使用 Mockito 和 JUnit 测试企业级应用程序。这是将产品添加到我拥有的产品 offline-repository-class-test 中的离线存储库类的方法的代码:
@Mock
private InitialData initialData;
@InjectMocks
private ProductRepositoryOffline pro;
@Test
public void testPersistProduct() {
Product product = new Product(0, "", "", "", 0.0, true, "", 0, /*Product type*/null, "", 0, 0);
ArrayList<Product> productList = new ArrayList<Product>();
//productList.add(product);
Mockito.when(initialData.getProducts()).thenReturn(productList);
pro.persistProduct(product);
assertEquals(pro.getProducts().get(0), product);
}
This relies on the following methods in classes:
这依赖于类中的以下方法:
The method it is testing in the ProductRepositoryOffline
:
它正在测试的方法ProductRepositoryOffline
:
@Override
public void persistProduct(Product pr) {
initialData.addProduct(pr);
}
InitialData
初始数据
private ArrayList<Product> products = new ArrayList<Product>();
public void addProduct(Product product) {
products.add(product);
}
The question I wish to ask is that in the case of pro.persistProduct(product)
unless I have product already added to the ArrayList
, isn't persistProduct
meant to be adding product to the arrayList without the need for the commented productList.add(product)
?
我想问的问题是,pro.persistProduct(product)
除非我已经将产品添加到ArrayList
,否则不persistProduct
打算将产品添加到 arrayList 而不需要评论productList.add(product)
?
回答by Draken
Here is what you should be doing:
这是你应该做的:
@Mock
private InitialData initialData;
@InjectMocks
private ProductRepositoryOffline pro;
@Test
public void testPersistProduct() {
Product product = new Product(0, "", "", "", 0.0, true, "", 0,
/*Product type*/null, "", 0, 0);
ArrayList<Product> productList = new ArrayList<Product>();
productList.add(product);
Mockito.when(initialData.getProducts()).thenReturn(productList);
pro.persistProduct(product);
assertEquals(pro.getProducts().get(0), product);
Mockito.verify(initialData).addProduct(product);
}
Because the object initialData
is mocked, when it calls the method initialData.addProduct(pr);
in your ProductRepositoryOffline
, it does nothing. You have to manually add it to the list for checking later in your assertEquals()
. To confirm the method was called though, you can use the verify()
method to check that the addProduct()
was called once on your mock object using the object of product
you created. You can see more examples of verify()
here
因为该对象initialData
是模拟的,所以当它调用initialData.addProduct(pr);
your 中的方法时ProductRepositoryOffline
,它什么也不做。您必须手动将其添加到列表中,以便稍后在您的assertEquals()
. 要确认该方法已被调用,您可以使用该verify()
方法检查是否addProduct()
使用product
您创建的对象在模拟对象上调用了一次。你可以在这里看到更多的例子verify()
There are other methods to mock void methods, like your usage of addProduct()
, to see some examples of those, see this question here
还有其他方法可以模拟 void 方法,例如您对 的使用addProduct()
,要查看其中的一些示例,请在此处查看此问题
[EDIT]Another variation you could do is to use the doAnswer()
, which would look something like this:
[编辑]你可以做的另一个变化是使用doAnswer()
,它看起来像这样:
Mockito.doAnswer(productList.add(product)).when(initialData).addProduct(product);
I'm not 100% this will work, as I have never used it, but I believe that at the point initialData.addProduct(product);
is called, then the product will be added to your product list. That way you wouldn't need to use productList.add(product);
Hope that helps a bit!
我不是 100% 这会起作用,因为我从未使用过它,但我相信在initialData.addProduct(product);
调用这一点时,该产品将被添加到您的产品列表中。这样你就不需要使用productList.add(product);
有点帮助的希望了!