Is it possible to mock void methods in a static class using PowerMOck 1.5.1?

1,696 views
Skip to first unread message

laredo...@zipmail.com

unread,
Jun 21, 2013, 3:05:21 PM6/21/13
to powe...@googlegroups.com
Hi,

I'm using Maven 3.0.3, PowerMock 1.5.1 and JUnit 4.8.1.  I've set up these dependencies ...

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-legacy</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-easymock</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>

My question is, how do I mock a void method from a static class?  I don't want any exception thrown, I just want nothing to happen.  I tried this …

        mockStatic(Transport.class);
        replay(Transport.class);
        boolean ret = m_emailSvc.sendEmail("m...@me.com",
                                           "y...@you.com",
                                           "localhost",
                                           "Test",
                                           "Test Body");

but the mock isn't invoked when this code is run

    public boolean sendEmail(final String toEmail,
                          final String fromEmail,
                          final String smtpHost,
                          final String subject,
                          final String body)
    {

        ...

           // Send message
           Transport.send(message);

Thanks for any guidance, - Dave

Matt Lachman

unread,
Jun 22, 2013, 1:48:41 PM6/22/13
to powe...@googlegroups.com
Based on the code snippet below, you need to set your expectations (expect or expectStatic, I can't remember since I switched to Mockito) between the mockStatic and replay calls.

Matt
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Johan Haleby

unread,
Jun 24, 2013, 1:48:00 AM6/24/13
to powe...@googlegroups.com
Also have a look at this blog

Regards,
/Johan

laredo...@zipmail.com

unread,
Jul 3, 2013, 9:37:12 AM7/3/13
to powe...@googlegroups.com
Johan,

I'm assuming you posted the link to the blog because I'm supposed to replace the static void method with one I create?  If so, what am I doing wrong with the below because the mock is still not being invoked ...

        mockStatic(Transport.class);
        replace(method(Transport.class, "send", Message.class)).
            with(method(MockTransport.class, "send"));

        replay(Transport.class);
        boolean ret = m_emailSvc.sendEmail("m...@me.com",
                                           "y...@you.com",
                                           "localhost",
                                           "Test",
                                           "Test Body");

    ...

    public static class MockTransport
    {
        public static void send(Message msg)
            throws MessagingException
        {
            System.out.println("invoked mock.");
        }
    }

What happens instead is the normal Transport class' send is getting called and an exception is being thrown

[ERROR]: org.collegeboard.subco.email.service.EmailServiceImpl - Could not connect to SMTP host: localhost, port: 25
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at org.collegeboard.subco.email.service.EmailServiceImpl.sendEmail(EmailServiceImpl.java:62)
    at org.collegeboard.subco.email.service.EmailServiceTest.testSendEmail(EmailServiceTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:228)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
    at java.net.Socket.connect(Socket.java:527)
    at java.net.Socket.connect(Socket.java:476)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    ... 31 more
Matt
        boolean ret = m_emailSvc.sendEmail("me@me.com",
                                           "y...@you.com",
                                           "localhost",
                                           "Test",
                                           "Test Body");

but the mock isn't invoked when this code is run

    public boolean sendEmail(final String toEmail,
                          final String fromEmail,
                          final String smtpHost,
                          final String subject,
                          final String body)
    {

        ...

           // Send message
           Transport.send(message);

Thanks for any guidance, - Dave

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+unsubscribe@googlegroups.com.

To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Johan Haleby

unread,
Jul 3, 2013, 9:45:26 AM7/3/13
to powe...@googlegroups.com
Well no I wasn't referring to replace method but rather suppress method since you said that "I don't want any exception thrown, I just want nothing to happen.". 

So you could do something like this:
 suppress(method(Transport.class, "send");

Then there's no need to mock or replay anything.

Perhaps what you're missing in your example is that you haven't prepared the Transport class for test using the @PrepareForTest annotation?

Regards,
/Johan

laredo...@zipmail.com

unread,
Jul 3, 2013, 10:13:32 AM7/3/13
to powe...@googlegroups.com
Ah thanks.  I'd left out the @PrepareForTest and @RunWith(PowerMockRunner.class) and after adding those, everything works. - Dave


On Friday, June 21, 2013 2:05:21 PM UTC-5, laredo...@zipmail.com wrote:
Hi,

I'm using Maven 3.0.3, PowerMock 1.5.1 and JUnit 4.8.1.  I've set up these dependencies ...

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-legacy</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-easymock</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>

My question is, how do I mock a void method from a static class?  I don't want any exception thrown, I just want nothing to happen.  I tried this …

        mockStatic(Transport.class);
        replay(Transport.class);
        boolean ret = m_emailSvc.sendEmail("me@me.com",
Reply all
Reply to author
Forward
0 new messages