gpt4 book ai didi

java - 测试用例未涵盖在 cobertura 中

转载 作者:行者123 更新时间:2023-12-01 08:55:42 25 4
gpt4 key购买 nike

public class MyUtil {
public static Properties loadProperties() throws Exception {
Properties prop = new Properties();
InputStream inputStream = MyUtil.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
if (inputStream != null) {
prop.load(inputStream);
}
return prop;
}
}

我已经为上述方法编写了测试用例,当我在 Eclipse 中作为测试用例运行时,它通过了,当我调试 loadProperties() 时,没有被调用,并且 cobertura 报告 显示为未覆盖的代码。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ MyUtil.class, Properties.class })

@Test
public void testLoadProperties() throws Exception{
String fileName = "application.properties";
Properties mockProps = PowerMockito.mock(Properties.class);
PowerMockito.mockStatic(Properties.class);
PowerMockito.whenNew(Properties.class).withNoArguments().thenReturn(mockProps);
InputStream mockInputStream = Mockito.mock(InputStream.class);
PowerMockito.mockStatic(MyUtil.class);
ClassLoader mockClassLoader = Mockito.mock(ClassLoader.class);
PowerMockito.when(MyUtil.class.getClassLoader()).thenReturn(mockClassLoader);
PowerMockito.when(mockClassLoader.getResourceAsStream(fileName)).thenReturn(mockInputStream);
PowerMockito.doNothing().when(mockProps).load((InputStream)Mockito.any());
MyUtil.loadProperties();
//assertNotNull("Not Null", MyUtil.loadProperties()); //assert failing
}

我应该更改什么才能确保我的代码确实覆盖了代码覆盖率?

最佳答案

PowerMock 破坏代码覆盖工具是一个众所周知的老问题:
https://github.com/cobertura/cobertura/issues/94

目前,只有一种方法可以获取代码覆盖率 JaCoCo 离线检测 https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo

关于java - 测试用例未涵盖在 cobertura 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com