簡體   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