JUnit方法级别类别不适用于PowerMockRunner

时间:2014-08-26 07:47:50

标签: junit powermock

鉴于此测试类

    @PowerMockIgnore("UnitTest")
    @Category(UnitTest.class)
    @RunWith(PowerMockRunner.class)
    public class SomeClass {

       @Test
       @Category(SlowTest.class)
       public void SlowMethod(){...}
    }

和测试套件

   @RunWith(Categories.class)
   @SuiteClasses({AllUnitTestSuite.class})
   @ExcludeCategory(SlowTest.class)
   public class FastUnitTestSuite {

   }

当我运行FastUnitTestSuite时,它运行SlowMethod,尽管它在SlowTest类别中。 我可以使用PowerMockIgnore和多个类别排除慢速测试类。如何在使用PowerMockRunner时排除慢速测试方法?

3 个答案:

答案 0 :(得分:1)

谢谢gontard!这是我改变的:

@PowerMockIgnore({ "org.mockito.*" })
@Category(UnitTest.class)

public class SomeClass {

   @Rule
   public PowerMockRule rule = new PowerMockRule(); //it has to be public

   @Test
   @Category(SlowTest.class)
   public void SlowMethod(){...}
}

我必须将这些库也添加到我的类路径中:powermock-module-junit4-rule,powermock-classloading-base,powermock-classloading-xstream,xpp3_min

答案 1 :(得分:1)

此问题归因于类别中的以下行。方法hasCorrectCategoryAnnotation

下的CategoryFilter文件
if (fIncluded == null || fIncluded.isAssignableFrom(each))
  • 即使类类型相同或继承,这也会失败,因为类别类加载器加载了fIncluded,而PowerMockRunner类加载器加载了each

可能我们需要修复这个逻辑

答案 2 :(得分:0)

如果您想将PowerMock与不同的测试运行员一起使用,则应使用powermock junit rule: PowerMockRule

您可以在SpringJUnit4ClassRunner中看到this answer的示例。