【发布时间】:2017-06-01 05:27:37
【问题描述】:
样品控制器
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注解中。
或者有人可以找到我解决声纳违规的解决方案。 提前致谢
【问题讨论】:
-
但是它需要更多关于静态和最终方法的信息。而且我还没有任何静态方法
标签: java junit sonarqube jacoco powermockito