How I can mock with Mockito other classes in my class which is under test?
For example:
MyClass.java
class MyClass {
public boolean perfo
You could refactor MyClass so that it uses dependency injection. Instead of having it create an AnythingPerformerClass instance you could pass in an instance of the class to the constructor of MyClass like so :
class MyClass {
private final AnythingPerformerClass clazz;
MyClass(AnythingPerformerClass clazz) {
this.clazz = clazz;
}
public boolean performAnything() {
return clazz.doSomething();
}
}
You can then pass in the mock implementation in the unit test
@Test
public void testPerformAnything() throws Exception {
AnythingPerformerClass mockedPerformer = Mockito.mock(AnythingPerformerClass.class);
MyClass clazz = new MyClass(mockedPerformer);
...
}
Alternatively, if your AnythingPerformerClass contains state then you could pass a AnythingPerformerClassBuilder to the constructor.