Can not mock behavior of final class and static intializers

609 views
Skip to first unread message

Kartik Kumar

unread,
Feb 18, 2010, 9:53:05 PM2/18/10
to powe...@googlegroups.com
public MimeMessage createMessage(Properties properties) {
        Session session = Session.getDefaultInstance(
properties, null);
        return new MimeMessage(session);
 }

This is my test method.
@Test(description = "test powermock mime message creation")
    public void testCreateMessage() throws Exception {
        Session realSession = Session.getDefaultInstance(this.properties, null);
        PowerMockito.mock(Session.class);

        PowerMockito.stub(PowerMockito.method(Session.class,
                "getDefaultInstance", Properties.class, Authenticator.class))
                .andReturn(realSession);
        MimeMessage message = PowerMockito.mock(MimeMessage.class);
        PowerMockito.whenNew(MimeMessage.class).withParameterTypes(Session.class)
                .withArguments(realSession).thenReturn(message);
        MimeHelper helper = new MimeHelper();
        MimeMessage testMessage = helper.createMessage(this.properties);
        Assert.assertNotNull(testMessage);
        PowerMockito.verifyStatic(Mockito.times(2));
        Session.getDefaultInstance(Mockito.isA(Properties.class),
                (Authenticator) Mockito.isNull());
        PowerMockito.verifyNew(MimeMessage.class).withArguments(Mockito.any(Session.class));
    }

I have prepared for test  MimeHelper: (Iclass to be instatiated) and (Session class) to

For the statement in bold, I have also tried the following options

1. PowerMockito.verifyNew(MimeMessage.class)withArguments(realSession);
2. PowerMockito.verifyNew(MimeMessage.class).withArguments(Mockito.isA(Session.class));

and I get the same error message. The funny thing is that this verification statement causes my other test to fail, the one that you helped me fix. If I remove this statement, my other test passes.


java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
......

Caused by: org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
-> at org.powermock.api.mockito.PowerMockito.verifyStatic(PowerMockito.java:155)

Johan Haleby

unread,
Feb 22, 2010, 4:00:32 PM2/22/10
to powe...@googlegroups.com
Don't know if this is the problem but you shouldn't prepare MimeMessage for test if this is the class you want to mock the construction of. You should prepare the class creating the instance of MimeMessage. E.g. if A creates a new instance of class B then you should prepare A for test.

/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

Reply all
Reply to author
Forward
0 new messages