简体   繁体   English

使用Mockito,如何验证方法是否具有某个参数?

[英]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.

相关问题 使用 Object 参数验证方法未使用 Mockito 调用 - Verify method with Object argument not called using Mockito 如何验证未使用 Mockito 调用特定方法? - How to verify that a specific method was not called using Mockito? 使用Mockito验证是否使用包含子字符串的参数调用了方法 - Using Mockito verify a method was called with an argument containing a substring Mockito验证使用正则表达式使用正确的参数调用方法 - Mockito verify that method is called with correct argument using regex 如何使用Mockito验证重载方法的调用次数? - How do I verify the number of invocations of overloaded method using Mockito? 如何使用 JUnit Mockito 验证未调用方法 - How to check that a method is not being called using JUnit Mockito Verify 如何使用Mockito验证未使用任何参数组合调用的模拟方法 - How to verify mocked method not called with any combination of parameters using Mockito 使用Mockito,如何验证我的lambda表达式被调用? - With Mockito, how do I verify my lambda expression was called? 如何验证Mockito是否使用期望的参数(以java.util.List作为参数)调用了方法 - How to verify if method was called with expected argument (java.util.List as an argument) by Mockito 如何验证mockito是否从其他方法调用了方法 - How to verify if method was called from other by mockito
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM