【发布时间】:2021-04-01 15:29:54
【问题描述】:
我有一个遗留代码,其中包含我要测试的以下方法。如果我删除 Collections.sort 行,那么测试用例就可以正常工作。但我正在尝试用它来执行测试用例。
void update(){
<!-- Other logic-->
Collections.sort(demographicsForms, DemographicsFormComparator.getInstance());
<!-- Other logic-->
}
demographicsForms 是一个包含基本 setter 和 getter 的 Pojo 类
而DemographicsFormComparator包含以下代码
public final class DemographicsFormComparator implements Comparator<DemographicsForm> {
public int compare(DemographicsForm demo1, DemographicsForm demo2) {
return demo1.getType().getCdfMeaning().compareTo(demo2.getType().getCdfMeaning());
}
private static DemographicsFormComparator INSTANCE = null;
public synchronized static DemographicsFormComparator getInstance() {
if (INSTANCE == null) {
INSTANCE = new DemographicsFormComparator();
}
return INSTANCE;
}
}
到目前为止,我已经尝试过了
@Mock
private DemographicsForm df1;
@Mock
private DemographicsForm df2;
@Test
public void test() {
final DemographicsFormComparator mockA = PowerMock.createMock(DemographicsFormComparator.class);
EasyMock.expect(mockA.compare(df1, df2)).andReturn(1).anyTimes();
PowerMock.mockStatic(DemographicsFormComparator.class);
EasyMock.expect(DemographicsFormComparator.getInstance()).andReturn(mockA).anyTimes();
PowerMock.replayAll(mockA);
}
但上述方法给了我以下错误
java.lang.AssertionError:
Unexpected method call DemographicsFormComparator.compare
编辑 1: 尝试为人口统计形式传递真实对象
DemographicsForm df1 = new DemographicsForm();
DemographicsForm df2 = new DemographicsForm();
final DemographicsFormComparator mockA = PowerMock.createMock(DemographicsFormComparator.class);
EasyMock.expect(mockA.compare(df1, df2)).andReturn(1).anyTimes();
PowerMock.mockStatic(DemographicsFormComparator.class);
EasyMock.expect(DemographicsFormComparator.getInstance()).andReturn(mockA).anyTimes();
PowerMock.replayAll();
仍然接到compare的意外电话
【问题讨论】:
-
为什么你需要任何个模拟框架呢?创建
demographicsForms的两个真实实例,将它们传递给update方法,断言update方法的副作用。除非你在update中显示“其他逻辑”——这很重要,这很难回答 -
其余代码包含可以轻松模拟和测试的更新逻辑,但由于我的代码包含这一行,因此模拟失败。如果我只是尝试传递对象,它将抛出
Null Pointer exception for comparemethod。 -
NullPointer表明您传递到update的对象没有应该存在的字段。从return demo1.getType().getCdfMeaning().compareTo(demo2.getType().getCdfMeaning())看不明显吗? -
我也试过这个
when(df1.getType()).thenReturn(codevalue); when(codevalue.getCdfMeaning()).thenReturn("test"); when(df2.getType()).thenReturn(codevalue1); when(codevalue1.getCdfMeaning()).thenReturn("test"); -
getType() 为
codevalue返回一个对象,其中还包含getCdfMeaning()
标签: java junit mockito powermock easymock