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);
}
}
我已经使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在模拟对生产代码进行的唯一调用。
换句话说,您的测试表明:
saveData()
时,伪造结果返回 truesaveData()
- 是的,结果是 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:
saveData()
, fake the result to return truesaveData()
- 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 thanLeaderboard
itself. If you must mock outsaveData()
, you should be testing the methods that callsaveData()
... check that they save the right data, that they act correctly whensaveData()
returns false, etc.如果我正确理解你的问题:
因为你在嘲笑 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.
添加运行器类为 MockitoJUnitRunner,请参考下面的示例代码,
现在代码覆盖率将会增加
add runner class as MockitoJUnitRunner, please refer the below sample code
now the code coverage will increase