Reputation: 317
I have this class called restTemplate. It is a RestTemplate object from the Spring Framework. I am trying to reset it in the beginning of a setup method for my Mockito test but I get the exception:
org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is null!
So what am I doing wrong? I have the correct packages listed under
<context:component-scan base-package = "..."/>
It is autowired into my test class and is listed in my applicationContext-test.xml file. What should I be looking at first?
Edit:
<bean id="restTemplate" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.web.client.RestTemplate" />
</bean>
Upvotes: 0
Views: 3139
Reputation: 32969
Per the API: Mockito.mock takes an instance of Class<?>
but in your spring config you are passing a String
that is the fully qualified name of a class but it is not a Class<?>
instance.
It begs the question, why are you setting up mocks in your Spring config? Why not just instantiate the class under test in your test and create the mock there. Mocks are designed you unit testing. If you are loading the context you are probably approaching integration testing. Not that loading a context in a JUnit test is a bad thing, but using Mocks in them seems to be mixing things.
HOWEVER, if you really want to try it, you might try something like declaring a bean that is the class instance using Class.forName as the factory method. Something like:
<bean id="classInstance" class="java.lang.Class" factory-method="forName">
<constructor-arg value="org.springframework.web.client.RestTemplate" />
</bean>
<bean id="restTemplate" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg ref="classInstance" />
</bean>
Upvotes: 3