- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我引用了PowerMock:Mock private method using PowerMockito并在这里应用了相同的逻辑。另外,我在 eclipse/STS 中安装了 EMMA(开源工具),但是当我运行代码时,我看到代码覆盖率为零。为什么?
public class MyClient {
public void publicApi() {
System.out.println("In publicApi");
int result = 0;
try {
result = privateApi("hello", 1);
} catch (Exception e) {
//Assert.fail();
}
System.out.println("result : "+result);
if (result == 20) {
throw new RuntimeException("boom");
}
}
private static int privateApi(String whatever, int num) throws Exception {
System.out.println("In privateAPI");
thirdPartyCall();
int resp = 10;
return resp;
}
private static void thirdPartyCall() throws Exception{
System.out.println("In thirdPartyCall");
//Actual WS call which may be down while running the test cases
}
}
MyClientTest.java
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClient.class)
public class MyClientTest {
@Test
public void testPublicAPI() throws Exception {
PowerMockito.mockStatic(MyClient.class);
//PowerMockito.doReturn(10).when(MyClient.class, "privateApi", anyString(), anyInt());
PowerMockito.when(MyClient.class,"privateApi", anyString(), anyInt()).thenReturn(anyInt());
}
}
pom.xml
<dependencies>
<!-- Power Mock -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
最佳答案
如果您正在构建 Spy 或 Mock,那么您不会调用被测试的实际代码。 spy 的目的是能够验证它们,以便通过调用正确的回调或方法来检查代码的行为是否正确。在模拟的情况下,重点是引导代码沿着特定的控制流路径前进,并verify()
与模拟的预期交互。
由于您的测试用例调用了 spy 的测试方法,因此您的代码覆盖率恰好为 0%也就不足为奇了。如果您要验证与模拟方法的交互,您可能会发现什么都没有发生。
您想要做的是设置模拟,但以“正常方式”调用测试下的实际代码。这个想法是准备执行环境,然后“正常”调用测试的方法调用,最后观察实际发生的情况。最后一点包括对生成的输出的正常断言、预期交互的验证(这些都发生了,而且还涉及预期的参数/值)。
更改您的测试代码:
MyClient classUnderTest = PowerMockito.spy(new MyClient());
致:
MyClient classUnderTest = new MyClient();
并观察代码覆盖率。
关于java - PowerMock + Emma - 私有(private)静态方法和其他方法的代码覆盖率显示为 0%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52064126/
在旧版 Web 应用程序中,我们没有对 javascript 部分进行任何单元测试。 我们希望提高代码质量并删除死代码。 有什么方法可以在人工测试期间在浏览器中进行逐行 Javascript 代码覆盖
我已经为我的 python 代码编写了测试,并且想检查测试覆盖了多少百分比,所以我决定使用 python 覆盖。但是我启动它时遇到问题。我用这个 bash 命令启动我的测试: export PYTHO
我正在使用 python 覆盖工具来运行我的单元测试。从结果中可以看出,它包括所有“站点包”。我怎样才能将它们排除在报告之外?我只想显示项目源代码的报告。 (ctrp) ubuntu@ubuntu-x
我正在尝试使用 Opencover 来运行 NUnit 测试用例的代码覆盖率。我得到了预期的结果,但问题是在执行 opencover 时它再次运行测试。我们不希望发生这种情况,因为我们单独运行测试。下
我正在尝试使用 Opencover 来运行 NUnit 测试用例的代码覆盖率。我得到了预期的结果,但问题是在执行 opencover 时它再次运行测试。我们不希望发生这种情况,因为我们单独运行测试。下
我有一个 python 项目,我使用: pipenv 毒性 pytest 还有更多。 基本上,我想将 tox 添加到我的 gitlab 管道中。几乎一切似乎都有效,从 tox 调用 mypy、flak
我使用Coverity来分析代码C。 配置命令: cov-configure --compiler /opt/toolchains/stbgcc-4.5.4-2.9/bin/mipsel-linux-
是否可以在运行时测量 python 代码覆盖率并在生成结果时查看结果?我尝试使用 coverage但找不到有帮助的选项。我最初的实验表明,.coverage 文件直到程序执行结束才被保存,这意味着我们
我有一个文件,test_basic.py: class TestCalculator(): def test_calculator(self): from basic impo
我有一个小型 PHP 项目,它使用 PHPUnit 进行单元测试和覆盖。我想生成 cobertura XML 格式的覆盖率报告。 我可以使用任何工具或插件来实现这一目标吗? 感谢任何帮助.. 最佳答案
我正在研究一个小 gem 并包含 simplecov到 spec_helper.rb 两行: require 'simplecov' SimpleCov.start 当我运行 rspec 测试时,si
go -cover 或 -coverprofile 在运行 go 测试时非常有用,并且可以在 html 或纯文本中很好地显示。但是是否有 api 以编程方式访问它或处理文件? 最佳答案 你可以试试ax
当我遇到代码覆盖问题时,我目前开始在 PHPUnit 中使用 strict 模式: 如果我使用setUp-方法创建我的类的新实例,__constructor-方法在我运行测试。 这是我的测试设置: p
我正在运行 EclEmma,它是 Eclipse 的 Emma 插件,覆盖率报告仅显示我定义的 Enum 的部分覆盖率,即使它显示 Enum 中唯一的值被覆盖。我假设支持 Enum 的隐含方法存在覆盖
我正在将 ANT 构建转换为 Maven。我不使用声纳。 在 Maven 中,Jacoco 似乎没有报告有关单元测试本身的覆盖率,而 ANT 则报告了。我也一直在尝试为我的 Maven 构建获取这个,
我正在努力为我正在处理的库实现 100% 的代码覆盖率,但我似乎对 switch 语句和覆盖率有一些问题,我根本不明白。 我目前使用的是 Jacoco 0.7.2,因为每个新版本似乎都与 Robole
有没有在intelliJ中不用浏览器运行代码覆盖率的方法? http://www.jetbrains.com/webstorm/webhelp/monitoring-code-coverage-for
我想确保我的 Django 测试套件涵盖我的 URL 配置中列出的所有 URL。有没有办法将 URL 配置中的列表与测试套件中命中的 URL 列表进行比较? 最佳答案 我能够通过定义自定义测试套件运行
哇,好乱啊。这是场景。 主干驱动的 JS 应用。 用于 AMD 功能的 RequireJS,初始化如下: 然后在 main.js 中添加以下配置代码: require.config( { p
我的问题很简单,但答案仍然难以捉摸。假设我有一个包裹 package mypackage func DoTheThing() int { return 5 } 现在假设我在 mypackage_t
我是一名优秀的程序员,十分优秀!