I'm trying to unit test ClassA(String)
constructor of following ClassA
class:
import ExternalObject;
class ClassA{
private ExternalObject externalObject;
public ClassA() {
super();
}
public ClassA(String string) {
super();
this.externalObject = new ExternalObject().parseString(string);
}
}
I want to mock the new ExternalObject().parseString(string)
call and stub it with a sample ExternalObject
object.
I've looked into Mockito, but it seems this task isn't possible with Mockito because:
- Mockito can only mock methods, not statements inside methods.
- Mockito requires object to be created before mocking them. Mockito needs an instance of the class before you can begin mocking. So even if I put
new ExternalObject().parseString()
call in a separate method in ClassA class, I can't call it without a ClassA instance.
Is it possible to do this in Mockito (or any other mocking library) without rewriting a lot of code? I'm open to small code changes to make it more test-friendly.