Java 单元测试-跳过对象构造函数和方法

Java 单元测试-跳过对象构造函数和方法,java,mockito,testng,powermockito,jmockit,Java,Mockito,Testng,Powermockito,Jmockit,我试图为一个冗长的方法编写一个单元测试,我希望跳过遗留的构造函数,这些构造函数将涉及大量的重构以进行测试。它们对后续测试没有影响 有没有办法忽略某些构造函数和方法 测试 @RunWith(PowerMockRunner.class) @PrepareForTest(TestThisClass.class) public class TestThisClassTest { @Tested TestThisClass testClass; String obj = "

我试图为一个冗长的方法编写一个单元测试,我希望跳过遗留的构造函数,这些构造函数将涉及大量的重构以进行测试。它们对后续测试没有影响

有没有办法忽略某些构造函数和方法

测试

@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);
     
    }
}

JMockit和PowerMock是完全不同的框架。您是否正在寻找这两方面的解决方案?根据您的示例,建议您删除jmockit标记