gpt4 book ai didi

java - 如何在静态方法上使用 Mockito.verify()?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:43 24 4
gpt4 key购买 nike

我正在研究 Junit 和 Mockito。在我的项目中,我有一个 SocialDataAccess Controller ,其代码如下所示:

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;
}

}

这就是我所做的。我提出了一个请求,我的请求成功地调用了我的 Controller 。当我尝试使用 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());

}
}

我得到的错误:

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).

我的问题是:

  1. 我可以在我的方法 removeOtherProviderAuthTokenFromSession(request,provider) 上使用 Mockito.verify() 吗?如果"is"如何? &如果“否”为什么? SocialUtility 是类名,方法是静态的。请求与到达 Controller 的请求相同。 provider 是一个字符串。我不想使用 PowerMockito。

  2. 我还想在 getOAuthServiceProvider(appCode, provider) 上使用验证。我该怎么做?

任何帮助将不胜感激。

最佳答案

使用 Mockito -> MockedStatic 验证静态方法。

如果方法有参数,你想验证它,那么会通过这种方式验证:

@Test
void testMethod() {
try (MockedStatic<StaticProperties> theMock = Mockito.mockStatic(StaticProperties.class)) {
theMock.when(StaticProperties.getProperty("abc", "xyz", "lmn"))).thenReturn("OK");

//code .....

theMock.verify(() -> StaticProperties.getProperty("abc", "xyz", "lmn"));
}

}

关于java - 如何在静态方法上使用 Mockito.verify()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34610042/

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