java Mockito 通过但代码覆盖率仍然很低
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3690033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito Passes but Code Coverage still low
提问by ferronrsmith
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!
我已经使用 mockito 来模拟一个类,但是当我使用代码覆盖率时,它没有检测到该方法被调用。难道我做错了什么?请帮忙!
回答by Jon Skeet
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!
- 当我打电话时
saveData()
,伪造结果返回真 - 现在打电话
saveData()
- 是的,结果是真的!
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 probablybe mocking out the dependencies of Leaderboard
rather than Leaderboard
itself. If you mustmock out saveData()
, you should be testing the methods that callsaveData()
... check that they save the right data, that they act correctly when saveData()
returns false, etc.
您可能应该嘲笑Leaderboard
而不是Leaderboard
它本身的依赖关系。如果您必须模拟saveData()
,您应该测试调用saveData()
...的方法,检查它们是否保存了正确的数据,当saveData()
返回 false时它们是否正确运行,等等。
回答by mhshams
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.
假设您想测试 A 类,但该类依赖于 B,而 B 在测试环境中实例化有点困难(出于任何原因)。在这种情况下,您可以模拟 B。
but here is your case you are mocking class A itself. that means you are not testing anything.
但这是你在嘲笑 A 类本身的情况。这意味着你没有测试任何东西。
回答by Bhaskara Arani
add runner class as MockitoJUnitRunner, please refer the below sample code
添加 runner 类为 MockitoJUnitRunner,请参考下面的示例代码
import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner.class)
public class MockitTesterClass{
@Mock
private TestService testServiceMock;
}
now the code coverage will increase
现在代码覆盖率会增加