我试图避免在JUnit测试中重复代码,但我有点卡住了.
这是我的第一个测试,对于第二个测试,它具有完全相同的方法但不同的服务(不同的输入).而不是TestCaseResourceTest1我有TestCaseResourceTest2.现在测试两者的正确方法是什么?我想为测试编号2提供一个单独的文件,我应该如何避免重复的代码? (例如,使用beforeFileTest()方法)
public class TestCaseResourceTest1 {
@Mock
private TestService testService;
@Mock
private AreaService areaService;
private TestCaseService1 testCaseService1; // is changed in test2
@Before
public void before() throws Exception{
testCaseService1 = mock(TestCaseService1.class); // is changed in test2
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); // is changed in test2
doReturn(false).when(areaService).chechExists(any(String.class), eq(false));
}
}
在test2中只更改注释的行是差异.
TNX