英文:
How to junit cover and enter the if condition of !CollectionUtils.isEmpty(disReadings)?
问题
我正在学习JUnit,尝试对这个类进行JUnit测试,但我无法通过代码中的if !CollectionUtils.isEmpty(disReadings)
部分进入代码:
List<DisLRead> disLReadList = new ArrayList<>();
cHlDisList.forEach(dis -> {
List<Double> disReadingsL = disRepository
.getReadsForDisId(dis.getDisId(), sDate, eDate);
if (!CollectionUtils.isEmpty(disReadingsL)) {
double max = Round.RoundToTwoDecimal(
disReadingsL.stream().mapToDouble(Double::doubleValue).max().getAsDouble());
double min = Round.RoundToTwoDecimal(
disReadingsL.stream().mapToDouble(Double::doubleValue).min().getAsDouble());
double avg = Round.RoundToTwoDecimal(
disReadingsL.stream().mapToDouble(Double::doubleValue).average().getAsDouble());
DisLReading disLReading = new DisLReading();
disLReading.setDisId(dis.getDisId());
disLReading.setDisName(dis.getDisName());
disLReading.setDisZ(dis.getDisZ());
disLReading.setMax(max);
disLReading.setMin(min);
disLReading.setAverage(avg);
disLReadList.add(disLReading);
}
});
更新:
我的测试类代码片段如下:
@Mock
DisRepository disRepository;
@Mock
Round round;
List<DisLRead> disLReadList = new ArrayList<>();
List<Double> disReadingsL = new ArrayList<>();
disReadingsL.add(100.0000);
disReadingsL.add(200.0000);
when(disRepository.getReadsForDisId(anyInt(), anyString(), anyString()))
.thenReturn(disReadingsL);
assertNotNull(disReadingsL);
assertEquals(!CollectionUtils.isEmpty(disReadingsL), true);
我做错了什么?如何进入代码的这部分?提前感谢!
英文:
I am learning junit and trying to junit test the class and I am not able to enter the if !CollectionUtils.isEmpty(disReadings)
part of code through the code:
List<DisLRead> disLReadList = new ArrayList<>();
cHlDisList.forEach(dis -> {
List<Double> disReadingsL = disRepository
.getReadsForDisId(dis.getDisId(), sDate, eDate);
if (!CollectionUtils.isEmpty(disReadingsL)) {
double max = Round.RoundToTwoDecimal(
disReadingsL.stream().mapToDouble(Double::doubleValue).max().getAsDouble());
double min = Round.RoundToTwoDecimal(
disReadingsL.stream().mapToDouble(Double::doubleValue).min().getAsDouble());
double avg = Round.RoundToTwoDecimal(
disReadingsL.stream().mapToDouble(Double::doubleValue).average().getAsDouble());
DisLReading disLReading = new DisLReading();
disLReading.setDisId(dis.getDisId());
disLReading.setDisName(dis.getDisName());
disLReading.setDisZ(dis.getDisZ());
disLReading.setMax(max);
disLReading.setMin(min);
disLReading.setAverage(avg);
disLReadList.add(disLReading);
}});
Update:
Snippet of my test class:
@Mock
DisRepository disRepository;
@Mock
Round round;
List<DisLRead> disLReadList = new ArrayList<>();
List<Double> disReadingsL = new ArrayList<>();
disReadingsL.add(100.0000);
disReadingsL.add(200.0000);
when(disRepository.getReadsForDisId(anyInt(), anyString(), anyString()))
.thenReturn(disReadingsL);
assertNotNull(disReadingsL);
assertEquals(!CollectionUtils.isEmpty(disReadingsL), true);
What am I doing wrong? how to enter the cover this part of code. Thanks in advance!
答案1
得分: 1
你有两种选择,一种是使用模拟框架,比如Mockito,为"disRepository"创建一个模拟对象,代码看起来像这样:
DisrepositoryClass disrepositoryMock = Mockito.mock(disrepository.class);
when(disrepositoryMock.getReadsForDisId(any(),any(),any()).thenReturn(List.of(Double.of(1d)));
另一种选择是创建你自己的模拟对象并提供它。
英文:
You have more or less two options either use a mocking framework like mockito to create a mock object for "disRepository" it would look like this:
DisrepositoryClass disrepositoryMock = Mockito.mock(disrepository.class);
when(disrepositoryMock.getReadsForDisId(any(),any(),any()).thenReturn(List.of(Double.of(1d)));
Alternativly you create your own Mock object and provide it.
答案2
得分: 0
CollectionUtils.isEmpty()
是一个静态方法,因此您必须使用PowerMockito。
mockStatic(CollectionUtils.class);
given(CollectionUtils.isEmpty(anyList())).willReturn(true);
英文:
CollectionUtils.isEmpty()
is a static method; therefore, you have to use PowerMockito.
mockStatic(CollectionUtils.class);
given(CollectionUtils.isEmpty(anyList())).willReturn(true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论