Today while working with Mockito and spring I got struck with this scenario,
public class MyClass {
private MyService myService;
int doSomethingElse(String str) {
.....
myService.doSomething(str);
...
}
}
public interface MyService {
String doSomething(String str);
}
public class Class1 {
private MyClass myClass;
public Stirng methodToBeTested() {
myClass.doSomethingElse("..");
}
}
public class class1Test {
@Mock
MyService myService;
@Resource
@Spy
@InjectMocks
MyClass myClass;
@Resource
@InjectMocks
Class1 class1;
public void setUPTest() {
MockitoAnnotations.initMocks(this);
}
@Test
public void methodToBeTestedTest() {
this.setUPTest();
...
class1.methodToBeTested();
}
}
Here I want to mock "MyService". But MyService is used in "MyClass" and it is used in "Class1" .
I want to initialise "MyClass" and "Class1" using spring.
When I try to run this test, I got the following exception
org.mockito.exceptions.base.MockitoException: Cannot mock/spy class $Proxy79 Mockito cannot mock/spy following: - final classes - anonymous classes - primitive types
Can anyone help me with this?