I want to test public methodToTest()
.
public class MyClass {
private String id;
public MyClass(String id) {}
public Object methodToTest() {
res = internalMethod();
// logic on res
return res;
}
private internalMethod() {
foo = LegacyClass(id);
res = foo.doSomething(); // DB operations here
return res;
}
}
public class LegacyClass {
private static DB db = DefaultDb();
private String id;
public LegacyClass(String id) {}
public Object doSomething() {
return db.foo(id);
}
}
In my test, I've tried:
- @Mock on DB: not work. because can not @InjectMocks on LegacyClass instance as it is created inside internal method
- @Spy on MyClass instance and doReturn-when on internalMethod (make it visible for test): not work. maybe it's some limitation on @Spy.
Is there any options except using PowerMock
?