I tried to avoid code duplication in the JUnit test, but I was kind of stuck.
This is my first test, for the second it has exactly the same methods, but different services (different input data). instead of TestCaseResourceTest1 , I have TestCaseResourceTest2 . Now, what could be the right way to test both? I want to have a separate file for test number 2, how do I avoid code duplication? (e.g. use beforeFileTest () method)
public class TestCaseResourceTest1 {
@Mock
private TestService testService;
@Mock
private AreaService areaService;
private TestCaseService1 testCaseService1;
@Before
public void before() throws Exception{
testCaseService1 = mock(TestCaseService1.class);
MockitoAnnotations.initMocks(this);
beforeFileTest();
}
private void beforeFileTest() throws Exception{
doReturn(true).when(areaService).chechExists(any(String.class), eq(false));
}
@Test
public void verifyFileExists() throws Exception{
verifyOtherArea(testCaseService1);
doReturn(false).when(areaService).chechExists(any(String.class), eq(false));
}
}
just the comment lines is changed in test2are the differences.
Tpx
source
share