If your test cases are in the same class, you can use a method annotated with @Before , for example:
... private DAO mockDAO; @Before public void setUp() { mockDAO = mock(DAO.class); when(mockDAO.a()).thenReturn("test"); ...etc... } ...
Or, if you need behavior across many test classes, you can write a utility class to set the behavior in the Mock instance, for example:
public class MockDAOPrototype { public DAO getMockWithDefaultBehaviour() { final DAO mockDAO = mock(DAO.class); when(mockDAO.a()).thenReturn("test"); ...etc... return mockDAO; } }
And then call MockDAOPrototype.getMockWithDefaultBehaviour() in your setUp method.
source share