I have the following scenario
interface DAO
{
String a();
String b();
String c();
}
I create a mock of this DAO interface and I feed it to something called DAOProcess. Inside DAOProcess, I have various methods calling DAO methods a, b and c.
Now each time I need to unit test a method in DAOProcess, I’ll end up writing when(mockDAO.a()).thenReturn("test").
Is there anyway I can move these when(mockDAO.a()).thenReturn("test") common to all the test cases ?
If your test cases are all in one class you could make use of a method annotated with
@Before, e.g.:Or, if you need the behaviour over many test classes you could write a utility class to set behaviour on a Mock instance, e.g.:
And then call
MockDAOPrototype.getMockWithDefaultBehaviour()in yoursetUpmethod.