gpt4 book ai didi

java - 添加在 @PrepareForTest 中编写 Junit 的类可以使 Sonar 覆盖率为零

转载 作者:行者123 更新时间:2023-11-30 06:56:30 26 4
gpt4 key购买 nike

示例 Controller

public class SampleController {

public void sampleMethod() {
ClassAbc classAbc = new ClassAbc();
classAbc.abcMethod();
//doStuff
}
}

Abc 类

public class ClassAbc {
public void abcMethod() {
//doStuff
}
}

用于 SampleController 的 Junit

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@PrepareForTest({SampleController.class})
public class SampleControllerTest {

@Autowired
SampleController sampleController;

public void setUp() {
ClassAbc classAbc = PowerMockito.mock(ClassAbc.class);
PowerMockito.whenNew(ClassAbc.class).withAnyArguments()
.thenReturn(classAbc);
doNothing().when(classAbc).abcMethod();
}

@Test
public void testsampleMethod() throws Exception {
sampleController.sampleMethod();
}
}

当我在@PrepareForTest中添加“SampleController.class”时,即:

@PrepareForTest({SampleController.class})

当我运行 Sonar 作业时,我收到以下违规信息。

14 more branches need to be covered by unit tests to reach the minimum threshold of 65.0% branch coverage.

这就是我对该类的覆盖率为零。

我想知道是否有必要将调用构造函数的类放入@PrepareForTest注释中,而不是正在构造的类中。

或者有人可以帮我找到解决 Sonar 违规问题的解决方案吗?提前致谢

最佳答案

PowerMockito wiki 中所述:

... right now there is NO WAY TO USE PowerMock with JaCoCo On-the-fly instrumentation. ... to get code coverage with JaCoCo - use offline Instrumentation ... You may find example of using PowerMock with JaCoCo Offline Instrumentation and Maven in our repository: jacoco-offline example.

关于java - 添加在 @PrepareForTest 中编写 Junit 的类可以使 Sonar 覆盖率为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41692245/

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