Mockito 通过,但代码覆盖率仍然较低

发布于 2024-09-19 03:01:00 字数 775 浏览 7 评论 0原文

package com.fitaxis.test;

import java.sql.SQLException;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static org.mockito.Mockito.*;

import com.fitaxis.leaderboard.LeaderBoard;

public class LeaderBoardTests {


 @Test 
 public void TestThatDataIsSavedToTheDatabase()
 {
  LeaderBoard leaderBoard = mock(LeaderBoard.class);
  //doNothing().doThrow(new RuntimeException()).when(leaderBoard).saveData();

  when(leaderBoard.saveData()).thenReturn(true);

  boolean res = leaderBoard.saveData();

  verify(leaderBoard).saveData();

  Assert.assertTrue(res);
 }

}

我已经使用mockito来模拟一个类,但是当我使用代码覆盖率时,它没有检测到该方法被调用。我做错了什么吗?请帮忙!

package com.fitaxis.test;

import java.sql.SQLException;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static org.mockito.Mockito.*;

import com.fitaxis.leaderboard.LeaderBoard;

public class LeaderBoardTests {


 @Test 
 public void TestThatDataIsSavedToTheDatabase()
 {
  LeaderBoard leaderBoard = mock(LeaderBoard.class);
  //doNothing().doThrow(new RuntimeException()).when(leaderBoard).saveData();

  when(leaderBoard.saveData()).thenReturn(true);

  boolean res = leaderBoard.saveData();

  verify(leaderBoard).saveData();

  Assert.assertTrue(res);
 }

}

I have used mockito to mock a class, but when I use code coverage it does not detect that the method as been called. Am I doing something wrong? Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

oО清风挽发oО 2024-09-26 03:01:00

看起来您正在模拟对生产代码进行的唯一调用。

换句话说,您的测试表明:

  • 当我调用 saveData() 时,伪造结果返回 true
  • 现在调用 saveData() - 是的,结果是 true!

据我所知,您的生产代码根本没有被调用。

模拟的目的是模拟生产类中的依赖关系,或者(有时,尽管我不喜欢)模拟生产类中您实际测试的代码将调用的某些方法。

您应该可能模拟Leaderboard的依赖项,而不是Leaderboard本身。如果您必须模拟saveData(),则应该测试调用 saveData()的方法。检查它们是否保存了正确的数据,以及当 saveData() 返回 false 时它们的行为是否正确,等等。

It looks like you're mocking out the only call you're making to production code.

In other words, your test says:

  • When I call saveData(), fake the result to return true
  • Now call saveData() - yay, the result was true!

None of your production code is being calls at all, as far as I can see.

The point of mocking is to mock out dependencies from your production class, or (sometimes, though I prefer not to) to mock out some methods of your production class that the code you're actually testing will call.

You should probably be mocking out the dependencies of Leaderboard rather than Leaderboard itself. If you must mock out saveData(), you should be testing the methods that call saveData()... check that they save the right data, that they act correctly when saveData() returns false, etc.

双手揣兜 2024-09-26 03:01:00

如果我正确理解你的问题:

因为你在嘲笑 LeaderBoard。这意味着您没有测试它。

如果你想测试 LeaderBoard,你应该测试实际的类而不是模拟的类。

假设您想测试 A 类,但该类依赖于 B,并且 B 在测试环境中实例化有点困难(出于任何原因)。在这种情况下,您可以模拟 B。

但在您的情况下,您正在模拟 A 类本身。这意味着您没有测试任何东西。

if i understand your question correctly :

because you are mocking LeaderBoard. that means that you are not testing it.

if you want to test LeaderBoard, you should test the actual class not the mocked one.

let say you want to test class A but this class depends on B and B is a bit difficult to instantiate in testing environment(for any reason). in such cases you can mock B.

but here is your case you are mocking class A itself. that means you are not testing anything.

疑心病 2024-09-26 03:01:00

添加运行器类为 MockitoJUnitRunner,请参考下面的示例代码,

import org.mockito.junit.MockitoJUnitRunner

@RunWith(MockitoJUnitRunner.class)
public class MockitTesterClass{
    @Mock
    private TestService testServiceMock;
}

现在代码覆盖率将会增加

add runner class as MockitoJUnitRunner, please refer the below sample code

import org.mockito.junit.MockitoJUnitRunner

@RunWith(MockitoJUnitRunner.class)
public class MockitTesterClass{
    @Mock
    private TestService testServiceMock;
}

now the code coverage will increase

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文