我正试图为一个冗长的方法编写一个单元测试,我希望跳过那些为了测试而涉及大量重构的遗留构造函数。它们对随后的测试没有影响。
有没有办法忽略某些构造函数和方法?
测试
@RunWith(PowerMockRunner.class)
@PrepareForTest(TestThisClass.class)
public class TestThisClassTest {
@Tested
TestThisClass testClass;
String obj = "test";
@Injectable
NeedToTestThis newCode;
@Test
public void testPost() throws Exception {
// still calling the SkipThisLegacyCode constructor
SkipThisLegacyCode mock = Mockito.mock(SkipThisLegacyCode.class);
whenNew(SkipThisLegacyCode.class).withAnyArguments().thenReturn(mock);
testClass.testPost(obj);
new Verifications() {{
// new code verifications
newCode.editStuff((String) any);
times = 1;
}};
}
}
代码
public class TestThisClass {
//some legacy code
@Inject
NeedToTestThis newCode;
public void testPost(String obj) throws Exception {
//legacy code
SkipThisLegacyCode skip = new SkipThisLegacyCode(obj); // code not relevant to test, throws java.lang.ExceptionInInitializerError
// new code
newCode.editStuff(obj);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!