[英]Using Mockito, how do I verify a method was a called with a certain argument?
I'm using Mockito 1.9.0. 我正在使用Mockito 1.9.0。 How would i verify that a method got called exactly once, and that one of the fields passed to it contained a certain value?
我如何验证一个方法只被调用一次,并且传递给它的一个字段包含一定的值? In my JUnit test, I have
在我的JUnit测试中,我有
@Before
public void setupMainProg() {
// Initialize m_orderSvc, m_opportunitySvc, m_myprojectOrgSvc
...
m_prog = new ProcessOrdersWorker(m_orderSvc, m_opportunitySvc, m_myprojectOrgSvc);
} // setupMainProg
@Test
public void testItAll() throws GeneralSecurityException, IOException {
m_prog.work();
}
The method "work" calls a method of "m_orderSvc" (one of the arguments passed in to the object). 方法“work”调用“m_orderSvc”方法(传递给对象的参数之一)。 "m_orderSvc," in turn contains a member field, "m_contractsDao".
“m_orderSvc”又包含一个成员字段“m_contractsDao”。 I want to verify that "m_contractsDao.save" got called exactly once and that the argument passed to it contains a certain value.
我想验证“m_contractsDao.save”只被调用一次,并且传递给它的参数包含一个特定值。
This may be a little confusing. 这可能有点令人困惑。 Let me know how I can clarify my question and I'm happy to do so.
让我知道如何澄清我的问题,我很高兴这样做。
First you need to create a mock m_contractsDao and set it up. 首先,您需要创建一个模拟m_contractsDao并进行设置。 Assuming that the class is ContractsDao:
假设该类是ContractsDao:
ContractsDao mock_contractsDao = mock(ContractsDao.class);
when(mock_contractsDao.save(any(String.class))).thenReturn("Some result");
Then inject the mock into m_orderSvc and call your method. 然后将模拟注入m_orderSvc并调用您的方法。
m_orderSvc.m_contractsDao = mock_contractsDao;
m_prog = new ProcessOrdersWorker(m_orderSvc, m_opportunitySvc, m_myprojectOrgSvc);
m_prog.work();
Finally, verify that the mock was called properly: 最后,验证模拟是否被正确调用:
verify(mock_contractsDao, times(1)).save("Parameter I'm expecting");
Building off of Mamboking's answer: 建立Mamboking的答案:
ContractsDao mock_contractsDao = mock(ContractsDao.class);
when(mock_contractsDao.save(anyString())).thenReturn("Some result");
m_orderSvc.m_contractsDao = mock_contractsDao;
m_prog = new ProcessOrdersWorker(m_orderSvc, m_opportunitySvc, m_myprojectOrgSvc);
m_prog.work();
Addressing your request to verify whether the argument contains a certain value, I could assume you mean that the argument is a String and you want to test whether the String argument contains a substring. 解决您的请求以验证参数是否包含某个值,我可以假设您的意思是该参数是一个String,并且您想要测试String参数是否包含子字符串。 For this you could do:
为此您可以这样做:
ArgumentCaptor<String> savedCaptor = ArgumentCaptor.forClass(String.class);
verify(mock_contractsDao).save(savedCaptor.capture());
assertTrue(savedCaptor.getValue().contains("substring I want to find");
If that assumption was wrong, and the argument to save()
is a collection of some kind, it would be only slightly different: 如果这个假设是错误的,并且
save()
的参数是某种类型的集合,那么它只会略有不同:
ArgumentCaptor<Collection<MyType>> savedCaptor = ArgumentCaptor.forClass(Collection.class);
verify(mock_contractsDao).save(savedCaptor.capture());
assertTrue(savedCaptor.getValue().contains(someMyTypeElementToFindInCollection);
You might also check into ArgumentMatchers , if you know how to use Hamcrest matchers. 如果您知道如何使用Hamcrest匹配器,您也可以检查ArgumentMatchers 。
这是更好的解决方案:
verify(mock_contractsDao, times(1)).save(Mockito.eq("Parameter I'm expecting"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.