Constructor Injected Class Instance Variable Loses Mocking doThrow or doReturn Doesn't work
See original GitHub issueI have a spring boot application and am using MockitoSugar to write tests. Spring boot version: 2.0.5.RELEASE Scala version: 2.11.8 mockito-scala_2.11: 1.2.4
Injecting mock in constructor:
val headerParamsResolverMock = mock[HeaderParamsResolver]
val irsArgumentResolver = new IRSArgumentResolver(headerParamsResolverMock)
Test Case:
"resolverArgument" should "invoke resolve method if header parameter is not resolved" in {
val parameter = mock[Parameter]
doReturn(parameter, Nil: _*).when(methodParameterMock).getParameter
doReturn("string", Nil: _*).when(parameter).getName
doReturn(false, Nil: _*).when(headerParamsResolverMock).isResolved
doReturn("string", Nil: _*).when(headerParamsResolverMock).resolve(mock[NativeWebRequest]) // doesn't work
irsArgumentResolver.resolveArgument(methodParameterMock, mock[ModelAndViewContainer], mock[NativeWebRequest],
mock[WebDataBinderFactory])
Mockito.verify(headerParamsResolverMock).resolve(mock[NativeWebRequest])
}
Class under test
@Component
public final class IRSArgumentResolver implements HandlerMethodArgumentResolver {
private HeaderParamsResolver headerParamsResolver;
public IRSArgumentResolver(HeaderParamsResolver headerParamsResolver) {
this.headerParamsResolver = headerParamsResolver;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) {
String sample = "";
if (!headerParamsResolver.isResolved())
sample = headerParamsResolver.resolve(webRequest);
System.out.println(sample); // here its null
return webRequest.getParameter(parameter.getParameter().getName());
}
}
If I see the debugger, the headerParamsResolver isn’t shown as a mock object like other method parameters show up. Like Mock for Method Paramater
OR Mock for ModelAndViewContainer
etc. Moreover, only the isResolved class instance variable is set but other mock features like doThrow or doReturn doesn’t work. PFA debugger screen.
Moreover, this test case throws exception:
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Mockito injection not working for constructor AND setter mocks ...
Constructor injection will NOT be used if the type of one or more of the parameters of the CHOSEN constructor is a primitive...
Read more >Mockito (Mockito 4.10.0 API) - javadoc.io
Shorthand for mocks creation - @Mock annotation. Minimizes repetitive mock creation code. Makes the test class more readable. Makes the verification error ...
Read more >Diff - platform/external/mockito - Google Git
+ * If non-mockable types are wanted, then constructor injection won't happen. ... + * Mockito doesn't mock final methods so the bottom...
Read more >Make services unit testable using dependency injection
To tell Spring DI that you want some dependencies to be injected into a class constructor, use another annotation called @Autowired . Okay, ......
Read more >Use Mockito to Mock Autowired Fields - DZone
I would suggest to use constructor injection instead. Dependency injection is very powerful feature of Inversion of Control containers like ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@gursahibsahni There are quite a few things wrong with this, let’s separate them in 2
mock
while you are verifying (line 57), I guess you probably want something likeHeaderParamsResolver
is being mocked properly, the fact that it looks like the real thing in the debugger is due to how theinline
mocker worksfalse
(I guess this is a typo as in the code above it does that)thanks for the instructions