Reputation: 1462
I have a simple use case wherein I need to create a mock object through spring. (I know there are better alternatives like @Mock
).
The Class which I need to mock say ClassToMock
is like this:
public classToMock {
public ClassToMock (String a, String b, int c)
...
}
//other methods omitted
}
In my Spring Bean I have:
<bean id="xyz" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="some val"/>
<constructor-arg value="some other val"/>
<constructor-arg type="int" value="2"/>
</bean>
I am getting:
No matching factory method founnd: factory method 'mock(String, String, int)'
Any suggestions?
Upvotes: 2
Views: 704
Reputation: 131346
I think you mix some concerns.
factory-method
attribute refers to a static method which returns the instance you want to create.
Besides, either you instantiate a org.mockito.Mockito
instance, either you instantiate a ClassToMock
instance. You cannot use parameters constructor of one (ClassToMock
class) to use them with the constructor or the other one (Mockito
class).
Besides, Mockito, alone cannot mock constructor. And why do you would need to mock the constructor ?
If you want to create a instance of your class with some predefined value for your test, you can declare it in your xml spring conf :
<bean id="xyz" class="ClassToMock" >
<constructor-arg index="0" value="some val"/>
<constructor-arg index="1" value="some other val"/>
<constructor-arg index="2" value="2"/>
</bean>
Upvotes: 1