如何对Junit进行覆盖并进入`!CollectionUtils.isEmpty(disReadings)`的条件?

huangapple go评论70阅读模式
英文:

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&lt;DisLRead&gt; disLReadList = new ArrayList&lt;&gt;();
cHlDisList.forEach(dis -&gt; {
List&lt;Double&gt; 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&lt;DisLRead&gt; disLReadList = new ArrayList&lt;&gt;();
	List&lt;Double&gt; disReadingsL = new ArrayList&lt;&gt;();
	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);

huangapple
  • 本文由 发表于 2020年8月3日 05:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63221099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定