我最近学习了如何使用PowerMock为名为Module的类编写单元测试,该类扩展了Base类。他们看起来像这样。

public class Base {
    protected final static ServiceA serviceA;
    protected final static ServiceB serviceB;
    static {
        serviceA = ServiceA.getInstance();
        serviceB = ServiceB.getInstance();
    }
}

public class Module extends Base {
    public DataA methodA() {
        return serviceA.getDataA();
    }
    public DataB methodB() {
        return serviceB.getDataB();
    }
}


我的单元测试如下所示:

@PowerMockIgnore("javax.management.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Module.class, ServiceA.class, ServiceB.class})
public class ModuleTest {
    private Module module;
    @Mock
    private ServiceA serviceA;
    @Mock
    private ServiceB serviceB;

    @Before
    public void setup() throws Exception {
        MockitoAnnotations.initMocks(this);

        PowerMockito.mockStatic(ServiceA.class);
        PowerMockito.when(ServiceA.getInstance).thenReturn(serviceA);

        PowerMockito.mockStatic(ServiceB.class);
        PowerMockito.when(ServiceB.getInstance).thenReturn(serviceB);

        module = new Module();
        // I spy it because it has other methods I need to mock
        module = PowerMockito.spy(module);
    }

    @Test
    public void methodATest() {
        DataA dataA = new DataA();
        PowerMockito.when(serviceA.getDataA()).thenReturn(dataA);
        DataA data = module.methodA();
        assertEquals(dataA, data);
    }
    @Test
    public void methodBTest() {
        DataB dataB = new DataB();
        PowerMockito.when(serviceB.getDataB()).thenReturn(dataB);
        DataB data = module.methodB();
        assertEquals(dataB, data);
    }
}


一切看起来都很简单,但是当我运行ModuleTest时,methodBTest()不会通过。似乎PowerMockito.when(serviceB.getDataB()).thenReturn(dataB)不起作用,并导致调用真正的serviceB.getDataB()方法。因此assertEquals(dataB, data)引发org.junit.ComparisonFailure

如果将methodBTest()放在methodATest()之前,则methodATest()不会通过。相同的原因。

如果将PowerMockito.when(serviceA.getDataA()).thenReturn(dataA)PowerMockito.when(serviceB.getDataB()).thenReturn(dataB)放在setup()中,则一切正常。

这整天都在我身边。有谁知道为什么会这样以及如何解决呢?我需要用各自的测试方法编写的模拟语句,因为我可能会更改返回的值。

最佳答案

这是一个几乎没有任何变化的解决方案

@PowerMockIgnore("javax.management.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Module.class, ServiceA.class, ServiceB.class})
public class ModuleTest {
    private Module module;

    private static ServiceA serviceA = Mockito.mock(ServiceA.class);

    private static ServiceB serviceB = Mockito.mock(ServiceB.class);

    @BeforeClass
    public static void oneTimeSetup() throws Exception {
        PowerMockito.mockStatic(ServiceA.class);
        PowerMockito.when(ServiceA.class, "getInstance").thenReturn(serviceA);

        PowerMockito.mockStatic(ServiceB.class);
        PowerMockito.when(ServiceB.class, "getInstance").thenReturn(serviceB);
    }

    @Before
    public void setup() throws Exception {
        module = new Module();
        // I spy it because it has other methods I need to mock
        module = PowerMockito.spy(module);
    }

    @Test
    public void methodATest() {
        DataA dataA = new DataA();
        Mockito.when(serviceA.getDataA()).thenReturn(dataA);
        DataA data = module.methodA();
        assertEquals(dataA, data);
    }
    @Test
    public void methodBTest() {
        DataB dataB = new DataB();
        Mockito.when(serviceB.getDataB()).thenReturn(dataB);
        DataB data = module.methodB();
        assertEquals(dataB, data);
    }
}


更改的内容(以及原因):


Base中:serviceAserviceB更改为受保护(如果私有,Module无法访问)
PowerMockito.when(ServiceA.class, "getInstance").thenReturn(serviceA);使用了“ proper”(AFAIK)语法
使用@BeforeClass并使serviceAserviceB静态以“绕过” Base中的静态初始化


已通过Junit 4.12,PowerMockito 1.6.2测试。



注意:也可以利用@SuppressStaticInitializationFor达到相同的目标:

@SuppressStaticInitializationFor(value = "so46196071.Base") // suppress the static in Base (note this is my package name)
@PowerMockIgnore("javax.management.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Module.class, ServiceA.class, ServiceB.class})
public class ModuleBisTest {
    private Module module;

    @Mock
    private ServiceA serviceA;

    @Mock
    private ServiceB serviceB;

    @Before
    public void setup() throws Exception {
        // MockitoAnnotations.initMocks(this); /* this is not needed => done by the runner */

        PowerMockito.mockStatic(ServiceA.class);
        PowerMockito.when(ServiceA.class, "getInstance").thenReturn(serviceA);

        PowerMockito.mockStatic(ServiceB.class);
        PowerMockito.when(ServiceB.class, "getInstance").thenReturn(serviceB);

        module = new Module();
        Whitebox.setInternalState(Base.class, "serviceA", serviceA); // set serviceA in Base "by hand"
        Whitebox.setInternalState(Base.class, "serviceB", serviceB); // set serviceB in Base "by hand"
        // I spy it because it has other methods I need to mock
        module = PowerMockito.spy(module);
    }

    // ...

10-04 11:35