My code is setup like this.
abstract class BaseController {
@Inject Store store;
}
class MyController extends BaseController {
private final Validator validator;
@Inject
public MyController(Validator validator) {
this.validator = validator;
}
public boolean someMethod() {
a = store.storingMethod();
b = validator.validate(a);
...
...
return true;
}
}
Now I wanted to write tests for myController
. In the test, I want to use the injected Store
but I want to mock out the Validator
.
I tried something like this.
@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest() {
private MyController myController;
@Mock private Validator validator;
@Before
public void before() {
myController = new MyController(validator);
}
}
I know, if I move the Store store
from the BaseController
to MyController
, I can initialize it in the constructor (just like I did for the validator). But, I want to have the Store in the Base class as it will be used by other classes extending it.
With the way my classes are set up, How do I inject the Store
while testing?