当我尝试编写单元测试时出现 AssertionError
AssertionError when I try to write a unit test
我正在创建处理器来收集数据并在列表中提供它们。但是当我尝试为我的方法创建测试时,我遇到了断言错误。我做错了什么?
我的Class:
@AllArgsConstructor
public class ZteProcessor implements OurProcessor {
private final static String VENDOR = "ZTE";
private String jsonString;
private Map<String, String> metricsGU;
private Map<String, String> metricsUMTS;
private Map<String, String> metricsLTE;
@Override
public List<TimingAdvance> getTA() throws ParseException, NotFoundPatternOrMetricsException {
TimeAdvanceDataStore data = new TimeAdvanceDataStore();
AllDataFromJSONFile fromJSONFile = ProcessorUtil.getAllData(jsonString);
if (jsonString.contains("String")) {
return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsGU, 2);
} else if (jsonString.contains("String-2")) {
return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsUMTS, 3);
} else if (jsonString.contains("String3")) {
return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsLTE, 4);
} else {
throw new NotFoundPatternOrMetricsException();
}
}
}
我的测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ProcessorUtil.class})
public class ZteProcessorTest {
@Mock
private AllDataFromJSONFile fromJSONFile;
@Mock
private TimeAdvanceDataStore data;
private OurProcessor processor;
private TimingAdvance timingAdvance = new TimingAdvance();
private Map<String, String> metricsGU = new HashMap<>();
private Map<String, String> metricsUMTS = new HashMap<>();
private Map<String, String> metricsLTE = new HashMap<>();
@Test
public void getTATest() throws Exception {
String jsonString = " { String : value}";
processor = new ZteProcessor(jsonString, metricsGU, metricsUMTS, metricsLTE);
List<TimingAdvance> list = new ArrayList<>();
list.add(timingAdvance);
PowerMockito.mockStatic(ProcessorUtil.class);
when(ProcessorUtil.getAllData(jsonString)).thenReturn(fromJSONFile);
when(data.allDataToTimingAdvance(jsonString, fromJSONFile, metricsGU, 2)).thenReturn(list);
assertEquals(list, processor.getTA());
}
}
堆栈跟踪:
java.lang.AssertionError:
Expected :[TimingAdvance{filial='null', vendor='null', cellName='null', periodDate=null, taMetrics=null}]
Actual :[]
<Click to see difference>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
我的 ZteProcessor 使用 class ProcessorUtill
的静态方法 getAllData(jsonString)
。为此,我使用了 powermock。
问题源于这样一个事实,即您将期望设置为 TimeAdvanceDataStore data
模拟,但您正在测试的方法中创建 TimeAdvanceDataStore
的新实例。
由于您已经在使用 PowerMockito,因此您可以像
这样利用新对象创建
PowerMockito.whenNew(TimeAdvanceDataStore.class)
.withAnyArguments().thenReturn(data);
最重要的是,想一想您的应用程序中有多少个 ZTEProcessor 和 TimeAdvanceDataStores。您是否总是希望在每次调用 getTA 时都有一个新的 TimeAdvanceDataStore 实例?
如果没有,只需在构造函数中传递 TimeAdvanceDataStore。
如果是,当您无法使用 PowerMockito 时,常见的方法是:
- 将 TimeAdvanceDataStore 工厂传递给 ZTEProcessor 构造函数
- 将 TimeAdvanceDataStore 传递给 getTA 方法
- 正在提取构造 TimeAdvanceDataStore 的方法并在测试中覆盖它
我正在创建处理器来收集数据并在列表中提供它们。但是当我尝试为我的方法创建测试时,我遇到了断言错误。我做错了什么?
我的Class:
@AllArgsConstructor
public class ZteProcessor implements OurProcessor {
private final static String VENDOR = "ZTE";
private String jsonString;
private Map<String, String> metricsGU;
private Map<String, String> metricsUMTS;
private Map<String, String> metricsLTE;
@Override
public List<TimingAdvance> getTA() throws ParseException, NotFoundPatternOrMetricsException {
TimeAdvanceDataStore data = new TimeAdvanceDataStore();
AllDataFromJSONFile fromJSONFile = ProcessorUtil.getAllData(jsonString);
if (jsonString.contains("String")) {
return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsGU, 2);
} else if (jsonString.contains("String-2")) {
return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsUMTS, 3);
} else if (jsonString.contains("String3")) {
return data.allDataToTimingAdvance(VENDOR, fromJSONFile, metricsLTE, 4);
} else {
throw new NotFoundPatternOrMetricsException();
}
}
}
我的测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ProcessorUtil.class})
public class ZteProcessorTest {
@Mock
private AllDataFromJSONFile fromJSONFile;
@Mock
private TimeAdvanceDataStore data;
private OurProcessor processor;
private TimingAdvance timingAdvance = new TimingAdvance();
private Map<String, String> metricsGU = new HashMap<>();
private Map<String, String> metricsUMTS = new HashMap<>();
private Map<String, String> metricsLTE = new HashMap<>();
@Test
public void getTATest() throws Exception {
String jsonString = " { String : value}";
processor = new ZteProcessor(jsonString, metricsGU, metricsUMTS, metricsLTE);
List<TimingAdvance> list = new ArrayList<>();
list.add(timingAdvance);
PowerMockito.mockStatic(ProcessorUtil.class);
when(ProcessorUtil.getAllData(jsonString)).thenReturn(fromJSONFile);
when(data.allDataToTimingAdvance(jsonString, fromJSONFile, metricsGU, 2)).thenReturn(list);
assertEquals(list, processor.getTA());
}
}
堆栈跟踪:
java.lang.AssertionError:
Expected :[TimingAdvance{filial='null', vendor='null', cellName='null', periodDate=null, taMetrics=null}]
Actual :[]
<Click to see difference>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
我的 ZteProcessor 使用 class ProcessorUtill
的静态方法 getAllData(jsonString)
。为此,我使用了 powermock。
问题源于这样一个事实,即您将期望设置为 TimeAdvanceDataStore data
模拟,但您正在测试的方法中创建 TimeAdvanceDataStore
的新实例。
由于您已经在使用 PowerMockito,因此您可以像
这样利用新对象创建PowerMockito.whenNew(TimeAdvanceDataStore.class)
.withAnyArguments().thenReturn(data);
最重要的是,想一想您的应用程序中有多少个 ZTEProcessor 和 TimeAdvanceDataStores。您是否总是希望在每次调用 getTA 时都有一个新的 TimeAdvanceDataStore 实例?
如果没有,只需在构造函数中传递 TimeAdvanceDataStore。
如果是,当您无法使用 PowerMockito 时,常见的方法是:
- 将 TimeAdvanceDataStore 工厂传递给 ZTEProcessor 构造函数
- 将 TimeAdvanceDataStore 传递给 getTA 方法
- 正在提取构造 TimeAdvanceDataStore 的方法并在测试中覆盖它