Problem with PowerMockRule vs PowerMockRunner

3,001 views
Skip to first unread message

Lars Francke

unread,
May 31, 2012, 9:14:44 AM5/31/12
to powe...@googlegroups.com
Hi,

I'm using Powermock for the first time so there's a realistic chance
I'm doing something wrong.

This is my minimal Test and it fails:

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.mockpolicies.Slf4jMockPolicy;
import org.powermock.core.classloader.annotations.MockPolicy;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.mockito.Mockito.verify;

@MockPolicy(Slf4jMockPolicy.class)
//@RunWith(PowerMockRunner.class)
public class MinimalTest {


@Rule
public PowerMockRule rule = new PowerMockRule();

@Test
public void testOne() {
Logger logger = LoggerFactory.getLogger(MinimalTest.class);
verify(logger).error("FOO");
}

@Test
public void testTwo() {
Logger logger = LoggerFactory.getLogger(MinimalTest.class);
verify(logger).error("FOO");
}
}

When I run the methods individually it works but if I run the full
class it does not work because LoggerFactory.getLogger doesn't return
a Mock object in the second (and all following) test methods.

If I use the PowerMockRunner instead of the Rule everything works as expected.

Any ideas? Seems like a bug to me but...well I might be wrong :)

Thanks for any help.

Cheers,
Lars

Johan Haleby

unread,
May 31, 2012, 3:11:51 PM5/31/12
to powe...@googlegroups.com
Hi Lars,

You're probably right that it's a bug. The PowerMockRule is experimental
and doesn't work in all cases. Also what implementation of the Rule are
you using? If you're using the classloader based rule you may want to
try the Java agent based rule and see if that works for you.

Regards,
/Johan

Lars Francke

unread,
Jun 7, 2012, 4:20:44 PM6/7/12
to powe...@googlegroups.com
Hi Johan,

sorry to reply this late, for some reason I didn't get your reply.

Thanks for "confirming" my suspicions.

I was following the documentation[1] and was using the powermock-classloading-xstream module. I'm not quite sure what you mean by Java agent based rule but we've settled on just using the PowerMockRunner in this case which works.

Cheers,
Lars

Johan Haleby

unread,
Jun 8, 2012, 1:36:34 AM6/8/12
to powe...@googlegroups.com
Hi, 

Glad you got it working, this is the Java agent based rule.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/rNI_VFRAZl4J.

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