繁体   English   中英

RunWith(PowerMockRunner.class)不适用于包注释

[英]RunWith(PowerMockRunner.class) does not work with package annotation

我试图让RunWith(PowerMockRunner.class)使用我现有的包注释。

版本:

powermock 1.4.12 mockito 1.9.0 junit 4.8.2

package-info.java //这是用于包注释的

@TestAnnotation(version="1.0")
package com.smin.dummy;

TestAnnotation.java //这是包“com.smin.dummy”的元数据注释类

package com.smin.dummy;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PACKAGE)
public @interface TestAnnotation {
  String version();
}

A.java

package com.smin.dummy;

public class A {
    private static Package myPackage;
    private static TestAnnotation version;

    static {
      myPackage = TestAnnotation.class.getPackage();
      version = myPackage.getAnnotation(TestAnnotation.class);
     }

    public static String getVersion() {
        return version.version();
    }
}

MockA.java

package com.smin.dummy;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.smin.dummy.A;

@RunWith(PowerMockRunner.class) //comment out this line to see the difference
@PrepareForTest(A.class)
public class MockA {
@Test
public void test_mocked() throws Throwable {
    String thisVersion = A.getVersion();
    System.out.println(thisVersion);
}
}

在unitest MockA.java中 ,如果我不使用RunWith(PowerMockRunner.class) ,我会按预期将thisVersion打印0.1 但是在添加RunWith(PowerMockRunner.class)thisVersion变为null 我怀疑PowerMockRunner在这里对包注释做了一些有趣的事情,任何人都有任何想法? 请参阅下面我的代码的迷你版本:

在@Alban的评论中进行调查,看起来将这个注释添加到您的测试用例中应该可以避免这个问题:

@PowerMockIgnore("com.smin.dummy.TestAnnotation")

暂无
暂无

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

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