让 Mockito 和 Powermock 正确抛出错误
我有以下代码
@PrepareForTest({Mongo.class, XYMongo.class, DB.class})
public class XYMongoTest extends UnitTest{
String host = Play.configuration.getProperty("mongo.host");
int port = Integer.parseInt(Play.configuration.getProperty("mongo.port"));
String name = Play.configuration.getProperty("mongo.name");
@Test
public void testRetrieveMongoDBSuccessful() throws UnknownHostException, MongoException, Exception
{
Mongo mongoMock = mock(Mongo.class);
DB mockDB = mock(DB.class);
PowerMockito.whenNew(Mongo.class).withArguments(host, port).thenReturn(mongoMock);
when(mongoMock.getDB(name)).thenReturn(mockDB);
XYMongo.getMongoDB();
verify(mongoMock.getDB(name));
}
@Test
public void testRetrieveMongoDBFailUnkownHost() throws Exception
{
try
{
PowerMockito.mockStatic(Mongo.class);
PowerMockito.whenNew(Mongo.class).withArguments(host, port).thenThrow(new UnknownHostException("Test Exception"));
XYMongo.getMongoDB();
PowerMockito.verifyNew(Mongo.class).withArguments(host, port);
}
catch (Exception e)
{
assertEquals("Test Exception", e.getMessage());
}
}}
第一个测试顺利通过,第二个测试失败,测试错误为
Failure,预期:<[Test Exception]>但是:<[此处缺少对 verify(mock) 的方法调用:->在 org.powermock.api.mockito.internal.inspirationcontrol.MockitoNewInvocalControl.expectSubstitutionLogic(MockitoNewInvocalControl.java:65) 正确验证示例:verify(mock).doSomething() 另外,此错误可能会出现,因为您验证了以下任一者:final /private/equals()/hashCode() 方法。这些方法无法被存根/验证。 ]>
关于如何解决这个问题有什么想法吗?尝试了我能想到的一切。
谢谢
保罗
I have the following code
@PrepareForTest({Mongo.class, XYMongo.class, DB.class})
public class XYMongoTest extends UnitTest{
String host = Play.configuration.getProperty("mongo.host");
int port = Integer.parseInt(Play.configuration.getProperty("mongo.port"));
String name = Play.configuration.getProperty("mongo.name");
@Test
public void testRetrieveMongoDBSuccessful() throws UnknownHostException, MongoException, Exception
{
Mongo mongoMock = mock(Mongo.class);
DB mockDB = mock(DB.class);
PowerMockito.whenNew(Mongo.class).withArguments(host, port).thenReturn(mongoMock);
when(mongoMock.getDB(name)).thenReturn(mockDB);
XYMongo.getMongoDB();
verify(mongoMock.getDB(name));
}
@Test
public void testRetrieveMongoDBFailUnkownHost() throws Exception
{
try
{
PowerMockito.mockStatic(Mongo.class);
PowerMockito.whenNew(Mongo.class).withArguments(host, port).thenThrow(new UnknownHostException("Test Exception"));
XYMongo.getMongoDB();
PowerMockito.verifyNew(Mongo.class).withArguments(host, port);
}
catch (Exception e)
{
assertEquals("Test Exception", e.getMessage());
}
}}
The first test passes fine and the second fails with the test error being
Failure, expected:<[Test Exception]> but was:<[ Missing method call for verify(mock) here: -> at org.powermock.api.mockito.internal.invocationcontrol.MockitoNewInvocationControl.expectSubstitutionLogic(MockitoNewInvocationControl.java:65) Example of correct verification: verify(mock).doSomething() Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. ]>
Any ideas on how to fix this? Tried everything I can think of.
Thanks
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误实际上来自
testRetrieveMongoDBSuccessful()
;看起来您的verify()
不太正确,但 Mockito 无法告诉您,直到下一次你与它互动。尝试将
testRetrieveMongoDBSuccessful()
的最后一行替换为:verify(mongoMock).getDB("name");
The error is actually coming from
testRetrieveMongoDBSuccessful()
; it looks like you've got theverify()
not-quite-right, but Mockito can't tell you that until the next time you interact with it.Try replacing the last line of
testRetrieveMongoDBSuccessful()
with:verify(mongoMock).getDB("name");