让 Mockito 和 Powermock 正确抛出错误

发布于 2024-12-06 12:00:40 字数 1627 浏览 1 评论 0原文

我有以下代码

@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 技术交流群。

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

发布评论

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

评论(1

┊风居住的梦幻卍 2024-12-13 12:00:40

该错误实际上来自 testRetrieveMongoDBSuccessful();看起来您的 verify() 不太正确,但 Mockito 无法告诉您,直到下一次你与它互动。

尝试将 testRetrieveMongoDBSuccessful() 的最后一行替换为:

verify(mongoMock).getDB("name");

The error is actually coming from testRetrieveMongoDBSuccessful(); it looks like you've got the verify() 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");

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