8

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がここのパッケージ注釈で面白いことをしているのではないかと思いますが、誰かが何か考えを持っていますか?以下の私のコードのミニバージョンを参照してください。

4

1 に答える 1

4

コメントでの@Albanの調査に基づいて、テストケースにこのアノテーションを追加すると、問題を回避できるようになります。

@PowerMockIgnore("com.smin.dummy.TestAnnotation")
于 2012-11-16T15:29:17.380 に答える