I am working on Junit & Mockito. In my project I have a SocialDataAccess Controller whose code goes like this:
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.
@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 method removeOtherProviderAuthTokenFromSession(request,provider)
. If "yes" How? & If "NO" why? SocialUtility
is 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?
Any Help would be appreciable.
To verify a static method using Mockito -> MockedStatic.
If the method has parameters and you want to verify it then it will be verify by this way:
@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"));
}
}
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 your SocialDataAccessController
Regarding your 2nd question:
I also want to use verify on getOAuthServiceProvider(appCode, provider). How can I do this?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.