使用 PowerMock 编写单元测试,模拟方法调用失败



我最近学会了使用 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);使用"正确"(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);
}
// ...

相关内容

  • 没有找到相关文章

最新更新