我正在研究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;
}
}
这就是我所做的。我已经提出请求,我的请求成功调用了我的控制器。当我尝试使用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).
我的问题是:
我可以在方法Mockito.verify()
上使用removeOtherProviderAuthTokenFromSession(request,provider)
吗?如果“是”怎么样? &安培;如果“不”为什么? SocialUtility
是类的名称,方法是静态的。请求是与控制器相同的请求。而提供者是一个字符串。我不想使用PowerMockito。
我还想在getOAuthServiceProvider(appCode, provider)
上使用验证。
我怎么能这样做?
任何帮助都会很明显。
答案 0 :(得分:2)
使用 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"));
}
}
答案 1 :(得分:1)
你必须使用PowerMockito单独使用Mockito才能验证这个
PowerMockito.doNothing().when(SocialUtility.class,
"removeOtherProviderAuthTokenFromSession",
any(MockHttpServletRequest.class),
anyString());
您可以在致电getSocialAuthServiceProvider()
SocialDataAccessController
或进行间谍活动
醇>
答案 2 :(得分:0)
关于你的第二个问题:
我还想在getOAuthServiceProvider(appCode,provider)上使用verify。我怎么能这样做?
答案可能是这样的:
Mockito.verify(this.getSocialAuthServiceProvider())
.getOAuthServiceProvider(Mockito.isA(String.class), Mockito.isA(String.class));
如果我遗失了某些内容,请告诉我。