Java 让Mockito和Powermock正确抛出错误

Java 让Mockito和Powermock正确抛出错误,java,testing,mockito,powermock,Java,Testing,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

我有以下代码

@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());
    }
}}
第一个测试顺利通过,第二个测试失败,测试错误为

失败,应为:但是是:在org.powermock.api.mockito.internal.invocationcontrol.MockitoNewInvocationControl.expectSubstitutionLogic(MockitoNewInvocationControl.java:65)正确验证的示例:verify(mock).doSomething()此外,此错误可能会出现,因为您验证了:final/private/equals()/hashCode()方法之一。无法对这些方法进行存根/验证。]>

有没有办法解决这个问题?我想尽了一切办法

谢谢


Paul

错误实际上来自
testRetrieveMongoDBSuccessful()
;看起来您的
verify()
不太正确,但是Mockito在下次与它交互之前无法告诉您这一点

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

验证(mongoMock).getDB(“名称”)