java 如何在静态方法上使用 Mockito.verify()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34610042/
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
How to use Mockito.verify() on static methods?
提问by Satyendra Singh
I am working on Junit & Mockito. In my project I have a SocialDataAccess Controller whose code goes like this:
我正在研究 Junit & Mockito。在我的项目中,我有一个 SocialDataAccess 控制器,其代码如下:
public class SocialDataAccessController implements Controller{
private SocialAuthServiceProvider socialAuthServiceProvider;
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String provider = request.getParameter("pId");
String appCode = request.getParameter("apc");
* check if data in session is of the same provider orof different
* provider, if different then remove auth and request token
**/
SocialUtility.removeOtherProviderAuthTokenFromSession(request,provider);
try {
/** creating the OAuthService object based on provider type **/
OAuthService service = getSocialAuthServiceProvider().getOAuthServiceProvider(appCode, provider);
.....
........
............
return new ModelAndView("redirect:callback.html?pId=" + provider);
}
public SocialAuthServiceProvider getSocialAuthServiceProvider() {
return socialAuthServiceProvider;
}
}
This is what I have done. I have made a request and my request successfully calls my controller. When I try to use Mockito.verify()to test whether my static method is called or not, I get an error as shown below.
这就是我所做的。我提出了一个请求,我的请求成功调用了我的控制器。当我尝试使用Mockito.verify()来测试是否调用了我的静态方法时,出现如下所示的错误。
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(
locations={
"file:/opt/div/BatchWorkspace/harvest_branch/WebContent/WEB-INF/test-servlet.xml"
}
)
public class TestSocialDataAccessController {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@SuppressWarnings("static-access")
@Test
public void testBasicSetUp() throws Exception{
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/social-connect.html")
.param("apc","tj")
.param("src","google")
.param("pId","ggl")
.param("cl","xxxxxxxxxxxxxx");
mockMvc.perform(requestBuilder)
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isMovedTemporarily())
.andExpect(MockMvcResultMatchers.redirectedUrl("xxxxxxxx"));
SocialUtility sutil = new SocialUtility();
SocialUtility spy = Mockito.spy(sutil);
MockHttpServletRequest request = requestBuilder.buildRequest(wac.getServletContext());
Mockito.verify(spy).removeOtherProviderAuthTokenFromSession(request,Matchers.anyString());
}
}
The error which I got:
我得到的错误:
org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at com.tj.harvest.testcase.TestSocialDataAccessController.testBasicSetUp(TestSocialDataAccessController.java:88)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
at com.tj.harvest.testcase.TestSocialDataAccessController.testBasicSetUp(TestSocialDataAccessController.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597).
My questions are:
我的问题是:
Can I use
Mockito.verify()on my methodremoveOtherProviderAuthTokenFromSession(request,provider). If "yes" How? & If "NO" why?SocialUtilityis the name of class and the method is static. Request is the same request which comes to the controller. And provider is a string. I don't want to use PowerMockito.I also want to use verify on
getOAuthServiceProvider(appCode, provider). How can I do this?
我可以
Mockito.verify()在我的方法上使用吗removeOtherProviderAuthTokenFromSession(request,provider)?如果“是”怎么办?&如果“不”为什么?SocialUtility是类名,方法是静态的。请求与控制器的请求相同。而 provider 是一个字符串。我不想使用 PowerMockito。我也想在
getOAuthServiceProvider(appCode, provider). 我怎样才能做到这一点?
Any Help would be appreciable.
任何帮助将是可观的。
回答by kuhajeyan
You have to use PowerMockito for this Mockito alone wont be able to verify this
PowerMockito.doNothing().when(SocialUtility.class, "removeOtherProviderAuthTokenFromSession", any(MockHttpServletRequest.class), anyString());You can mock your
getSocialAuthServiceProvider()or spy it when you call yourSocialDataAccessController
您必须为此 Mockito 使用 PowerMockito 单独无法验证这一点
PowerMockito.doNothing().when(SocialUtility.class, "removeOtherProviderAuthTokenFromSession", any(MockHttpServletRequest.class), anyString());getSocialAuthServiceProvider()当你打电话给你时,你可以嘲笑或窥探它SocialDataAccessController
回答by Nikunj
Regarding your 2nd question:
关于你的第二个问题:
I also want to use verify on getOAuthServiceProvider(appCode, provider). How can I do this?
我还想在 getOAuthServiceProvider(appCode, provider) 上使用验证。我怎样才能做到这一点?
Answer may be like this:
答案可能是这样的:
Mockito.verify(this.getSocialAuthServiceProvider())
.getOAuthServiceProvider(Mockito.isA(String.class), Mockito.isA(String.class));
Let me know if I'm missing something.
如果我遗漏了什么,请告诉我。

