简体   繁体   English

将PowerMockRunner与Junit Test Suite结合使用

[英]Using PowerMockRunner with Junit Test Suite

I'm trying to create a Junit test suite along with using PowerMockRunner but it does not work. 我正在尝试使用PowerMockRunner创建一个Junit测试套件,但是它不起作用。

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(MainTest.class)
@Suite.SuiteClasses({ MainTest.Class1Test.class })
@PrepareForTest({
    StaticFieldsProvider.class
})
public class MainTest extends Suite {

public MainTest(Class<?> klass, RunnerBuilder builder)
        throws InitializationError {
    super(klass, builder);
}

public static class TestBase {
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        PowerMockito.mockStatic(StaticFieldsProvider.class);
    }
}

public static class Class1Test extends TestBase {
    @Before
    public void setUp() {
        super.setUp();
    }

    @Test
    public void test(){
        assertTrue(true);
    }
 }
}

When I try to run, it fails with error - 当我尝试运行时,它失败并显示错误-

java.lang.IllegalArgumentException: Test class can only have one constructor at org.junit.runners.model.TestClass.(TestClass.java:40) java.lang.IllegalArgumentException:测试类在org.junit.runners.model.TestClass上只能有一个构造函数。(TestClass.java:40)

Any suggestions on how to use PowerMockRunner in above case? 关于在上述情况下如何使用PowerMockRunner任何建议?

Thanks 谢谢

This is an old question, so we may get no resolution on whether or not this solution works for the OP; 这是一个古老的问题,因此我们可能无法确定该解决方案是否适用于OP。 but this might work (I can't verify without having access to StaticFieldsProvider , but it works if I swap that out with one of my own classes). 但这可能有效(如果无法访问StaticFieldsProvider ,我将无法验证,但是如果我将其与自己的一个类交换出去,它将可以正常工作)。 I would love for someone to edit and add more explanation as to why this works: 我希望有人可以编辑并添加更多有关为何有效的解释:

@RunWith(PowerMockRunner.class)
// * Delegate to Suite.class instead of MainTest.class *
@PowerMockRunnerDelegate(Suite.class)
@Suite.SuiteClasses({ MainTest.Class1Test.class })
@PrepareForTest({
        StaticFieldsProvider.class
})
// * Don't extend Suite *
public class MainTest {

// * Remove constructor *

    public static class TestBase {
        @Before
        public void setUp() {
            MockitoAnnotations.initMocks(this);
            PowerMockito.mockStatic(StaticFieldsProvider.class);
        }
    }

    public static class Class1Test extends TestBase {
        @Before
        public void setUp() {
            super.setUp();
        }

        @Test
        public void test(){
            assertTrue(true);
        }
    }
}

In case it helps someone else, I had a slightly different scenario in that only a couple of the classes in my suite need PowerMockRunner (and don't mock out the same thing, so the mock needs to happen in each individual test class instead of in the runner). 万一它对其他人有帮助,我的情况稍有不同,我的套件中只有几个类需要PowerMockRunner (并且不要模拟相同的东西,因此模拟需要在每个单独的测试类中进行,而不是在每个测试类中进行)在跑步者中)。 It appears that as long as I @PrepareForTest in my runner (as above) the classes I will need in some of the test classes, I can still create the mocks in the @Before (or wherever) of the applicable test class. 看起来,只要我在跑步者(如上)中使用@PrepareForTest (如上所述)某些测试类中所需的类,我仍然可以在适用测试类的@Before (或任何位置)中创建@Before Hope this helps. 希望这可以帮助。

您不能扩展Suite ,因为它是JUnit 3的一部分,并且您正在使用JUnit4。(除去extends和构造函数。)有关JUnit 4中Suites的更多信息 ,请参见JUnit Wiki

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM