我正在为我的测试类使用PowerMockito和@PrepareForTest注释。当我这样做时,声纳说没有任何分支被覆盖。但是,我的其他不使用PowerMockito的测试类运行良好。这是我的pom看起来像
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.2 </version>
<scope>test</scope><plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<!-- <phase>prepare-package</phase> -->
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</dependency>
我需要在这个pom中添加任何东西吗? 任何帮助都是赞赏的,在此先感谢
答案 0 :(得分:1)
Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
@PrepareOnlyThisForTest
,则可以解决此问题,这提供了隔离特定测试类的能力。@PowerMockRunnerDelegate(SpringRunner.class)
在 pom.xml 中添加 Power Mock 依赖
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
将 PowerMock Junit Runner 用于测试类并声明我们正在模拟的测试类并委托 SpringRunner
@RunWith(PowerMockRunner.class)
@PrepareOnlyThisForTest(ServiceApplication.class)
@PowerMockRunnerDelegate(SpringRunner.class)
public class ExampleControllerTest {
PowerMockito.mockStatic(ServiceApplication.class);
Mockito.when(ServiceApplication.getStatic()).thenReturn("");
答案 1 :(得分:0)
是的,正确的powermock第一步无法与声纳配合使用,问题的原因是powermock和jacoco的结合, 已经存在与此问题相关的问题(https://github.com/powermock/powermock/issues/727),有关更多信息,请参见此处:
https://github.com/powermock/powermock/wiki/Code-coverage-with-JaCoCo
同时,您可以尝试解决方法,如下所述:
答案 2 :(得分:0)
我发现如果@PrepareForTest批注中只有一个类,例如
@PrepareForTest(ClassA.class)
,然后jacoco覆盖范围将起作用。只有当一个以上的班级例如@PrepareForTest({ClassA.class, ClassB.class})
报告jacoco覆盖率为0%
答案 3 :(得分:-1)
您只需要将Runner代理添加到SpringRunner;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)