In my class under test, I have code snippet which codes something
like this:
Properties p = new Properties();
p.put("mail.smtp.host", smtpHost);
Session session = Session.getDefaultInstance(p, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(....)
msg.setTo(...)
msg.setText("...");
msg.setSubject("..");
Transport.send(msg);
I have two questions
1. Is there a way to mock MimeMessage considering that it depends on
session as a constructor argument. PowerMockito#mock(Class<T> class)
accepts only one argument.
2. Transport#send(MimeMessage) has a void return type. To mock void
return types, PowerMockito.doNothing() .when(T)... is used. But T
expects an object not a static class.
Mock code:
/ Mock session classes.
MimeMessage message = PowerMockito.mock(MimeMessage.class);
PowerMockito.doNothing().when(message)
.setFrom(Mockito.isA(InternetAddress.class));
PowerMockito.doNothing().when(message).setRecipient(
Mockito.eq(Message.RecipientType.TO),
Mockito.isA(InternetAddress.class));
PowerMockito.doNothing().when(message).setSubject(Mockito.anyString());
PowerMockito.doNothing().when(message)
.setSentDate(Mockito.any(Date.class));
PowerMockito.doNothing().when(message).setText(Mockito.anyString());
PowerMockito.mockStatic(Transport.class);
Please advise.
PowerMockito.doNothing().when(Transport.class); Transport.send(msgMock);
--
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.
This means that PowerMock cannot determine the constructor based on the parameters you sent. In these cases (which ought to be quite rare) you need to specify the parameter types as well e.g.
whenNew(MimeMessage.class).withParameterTypes(MyParameterType.class)I .withArguments(isA(MyParameter.class)).thenReturn(mimeMessageMock);
On Feb 8, 6:13 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Sorry my mistake. New instance verification of overloaded constructors is
> not actually implemented yet in the Mockito API. As a work-around, could you
> try casting the arguments you pass into "withArguments" to the concrete
> parameter type(s)?
>
> /Johan
>
> On Mon, Feb 8, 2010 at 9:24 AM, Kartik Kumar <krishnan.1...@gmail.com>wrote:
>
> > Hi Johan,
>
> > I am sorry if I am missing something. I started using Powermock about 3
> > days so I may be making rudimentary mistakes.
>
> > PowerMockito#verifyNew(Class<?> class) returns an object of
> > ConstructorArgumentVerification. The strategy implementation only exposes
> > withArguments(Object, Object...) or withNoArguments(). I have tried to pass
> > realSession as an argument and Mockito matchers such as
> > Mockito.eq(realSesession) and Mockito.isA(Session.class). I get the same
> > error.
>
> > Thanks,
>
> > Kartik
>
> > On Sun, Feb 7, 2010 at 11:17 PM, Johan Haleby <johan.hal...@gmail.com>wrote:
>
> >> It's perhaps the same problem with verifyNew as with whenNew, you need to
> >> specify the parameter types as well otherwise the wrong constructor may be
> >> verified in your case.
>
> >> /Johan
>
> >>> On Thu, Feb 4, 2010 at 11:27 PM, Johan Haleby <johan.hal...@gmail.com>wrote:
>
> >>>> This means that PowerMock cannot determine the constructor based on the
> >>>> parameters you sent. In these cases (which ought to be quite rare) you need
> >>>> to specify the parameter types as well e.g.
> >>>> whenNew(MimeMessage.class).withParameterTypes(MyParameterType.class)I
> >>>> .withArguments(isA(MyParameter.class)).thenReturn(mimeMessageMock);
>
> >>>> /Johan
>
> >>>> On Fri, Feb 5, 2010 at 1:53 AM, Kartik Kumar <krishnan.1...@gmail.com>wrote:
>
> >>>>> Hi Johan,
>
> >>>>> I tried your suggestion and I am getting an error message that several
> >>>>> matching constructors are found. I am using TestNG and Mockito.
>
> >>>>> *Test Code*
>
> >>>>> // Mock session classes.
>
> >>>>> MimeMessage message = PowerMockito.mock(MimeMessage.class);
> >>>>> PowerMockito.whenNew(MimeMessage.class)
> >>>>> .withArguments(Mockito.isA(Session.class))
> >>>>> .thenReturn(message);
>
> >>>>> // These lines are not executed.
>
> >>>>> PowerMockito.doNothing().when(message)
> >>>>> .setFrom(Mockito.isA(InternetAddress.class));
> >>>>> PowerMockito.doNothing().when(message).setRecipient(
> >>>>> Mockito.eq(Message.RecipientType.TO),
> >>>>> Mockito.isA(InternetAddress.class));
>
> >>>>> PowerMockito.doNothing().when(message).setSubject(Mockito.anyString());
> >>>>> PowerMockito.doNothing().when(message)
> >>>>> .setSentDate(Mockito.any(Date.class));
>
> >>>>> PowerMockito.doNothing().when(message).setText(Mockito.anyString());
> >>>>> PowerMockito.mockStatic(Transport.class);
> >>>>> PowerMockito.doNothing().when(Transport.class);
>
> >>>>> // Call class under test.
> >>>>> swEaiEmail eaiEmail = new swEaiEmail();
> >>>>> eaiEmail.execute("", Collections.EMPTY_LIST, inputFields);
> >>>>> Transport.send(message);
>
> >>>>> *Error Log*
> >>>>> Caused by: *org.powermock.reflect.exceptions.TooManyConstructorsFoundException:
> >>>>> Several matching constructors found, please specify the argument parameter
> >>>>> types so that PowerMock can determine which method you're refering to.
> >>>>> *
> >>>>> Matching constructors in class javax.mail.internet.MimeMessage were:
> >>>>> javax.mail.internet.MimeMessage( javax.mail.Folder.class int.class )
> >>>>> javax.mail.internet.MimeMessage( javax.mail.Session.class
> >>>>> java.io.InputStream.class )
>
> >>>>> at
> >>>>> org.powermock.reflect.internal.WhiteboxImpl.throwExceptionWhenMultipleConstructorMatchesFound(WhiteboxImpl.java:1408)
> >>>>> at
> >>>>> org.powermock.reflect.internal.WhiteboxImpl.findUniqueConstructorOrThrowException(WhiteboxImpl.java:904)
> >>>>> at
> >>>>> org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubsituteMock(DefaultConstructorExpectationSetup.java:69)
> >>>>> at
>
> ...
>
> read more »
--
Should be:PowerMockito.verifyNew(Properties.class)
PowerMockito.verifyNew(Properties.class).withNoArguments();