【问题标题】:Running PowerMock with Enclosed class使用封闭类运行 PowerMock
【发布时间】:2013-10-15 18:55:23
【问题描述】:

我在 pararell 中使用 @Runwith(Enclosed.class) 运行 PowerMock 时遇到问题。

我的测试类结构:

@RunWith(PowerMockRunner.class)
@PrepareForTest(UnitBuild.class)

public class ut_QueueBuild{

    @Test
    public void someTest(){}

    public static InnerTestClass{

        @Test
        public void someInnerTest(){}
    }

}

在使用powerMock之前我只是用过:

@RunWith(Enclosed.class)
public class unitTestClass {
...

但由于我使用的是 PowerMock,所以无法在 pararell 中使用 @Runwith(PowerMockRunner.class) 和 @Runwith(Enclosed.class)

我该如何解决这个问题?

非常感谢!

【问题讨论】:

    标签: java unit-testing annotations junit4


    【解决方案1】:

    其实你可以使用PowerMockRunnerDelegate 做:

    @RunWith(PowerMockRunner.class)
    @PowerMockRunnerDelegate(Enclosed.class)
    @PrepareForTest(MyStaticClass.class)
    public class ut_QueueBuild{
    
        @Test
        public void someTest(){}
    
        public static InnerTestClass{
    
            @Test
            public void someInnerTest(){}
        }
    
    }
    

    这也适用于其他跑步者,例如Parametrized

    【讨论】:

      【解决方案2】:

      JUnit4只支持一个@RunWith注解,JUnit4的@RunWith注解不接受多个runner。Reference: project13

      可能有重复的问题,但这是StackOverflow Question@Matthew Farwell 的答案

      不,您需要:

      使用一个并创建一个测试基类来做你想做的事情 其他跑步者要做。将您的测试分成多个测试,每个 使用不同的跑步者。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2016-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        相关资源
        最近更新 更多