PowerMockPolicy - Not applied to mock's?

56 views
Skip to first unread message

ron.difrango

unread,
Feb 21, 2012, 12:55:50 PM2/21/12
to PowerMock
I created a mock policy over 1 of my interface classes like the
following:

Method getDataMethod = Whitebox.getMethod(LoggerManager.class,
"getLogger", Class.class);
Logger loggerMock = PowerMock.createNiceMock(Logger.class);
settings.stubMethod(getDataMethod, loggerMock);

And this works just fine if I new up the underlying implementation
class. But if I do the following in my test class:

@MockNice
LoggerManager loggerManager;

I get a NULL return value when I execute the following:

Logger logger =
loggerManager.getLogger(EpfLoggingMockPolicyTest.class);

Thoughts on why this is and ways around this? I guess I could
possibly create my own listener via PowerMockTestListener

Johan Haleby

unread,
Feb 21, 2012, 1:44:50 PM2/21/12
to powe...@googlegroups.com
Logger logger =
loggerManager.getLogger(EpfLoggingMockPolicyTest.class);

When is this executed? Is it in a method? In a static context?

/Johan

ron.difrango

unread,
Feb 21, 2012, 1:48:49 PM2/21/12
to PowerMock
Ah, good question. It is executed with the context of test like so:

@Test
public void assertThatMyFirstMockPolicyWork() throws Exception {
Logger logger =
loggerManager.getLogger(EpfLoggingMockPolicyTest.class);
assertThat("Expected a host name match", logger,
instanceOf(Proxy.class));
logger.debug("Testing");

Johan Haleby

unread,
Feb 21, 2012, 2:04:08 PM2/21/12
to powe...@googlegroups.com
Hmm can't see what's wrong. Can you paste the entire test case and the
mock policy implementation?

/Johan

ron.difrango

unread,
Feb 21, 2012, 2:11:49 PM2/21/12
to PowerMock
Policy:

public class EpfLoggingMockPolicy implements PowerMockPolicy {
private static final String[] QUALIFIED_NAMES =
{"com.capitalone.epf.logging.*"};

@Override
public void applyClassLoadingPolicy(MockPolicyClassLoadingSettings
settings) {

settings.addFullyQualifiedNamesOfClassesToLoadByMockClassloader(QUALIFIED_NAMES);
}

@Override
public void applyInterceptionPolicy(MockPolicyInterceptionSettings
settings) {
// Setup up the impl class
Method getDataMethod = Whitebox.getMethod(LoggerManagerImpl.class,
"getLogger", Class.class);
Logger loggerMock = PowerMock.createNiceMock(Logger.class);
settings.stubMethod(getDataMethod, loggerMock);
// setup the interface
getDataMethod = Whitebox.getMethod(LoggerManager.class,
"getLogger", Class.class);
settings.stubMethod(getDataMethod, loggerMock);
}
}

Test:

@RunWith(PowerMockRunner.class)
@MockPolicy(EpfLoggingMockPolicy.class)
public class EpfLoggingMockPolicyTest {
@Test
public void assertThatMyFirstMockPolicyWork() throws Exception {
// This test works
Logger logger = new LoggerManagerImpl()
.getLogger(EpfLoggingMockPolicyTest.class);
assertThat("Expected a host name match", logger,
instanceOf(Proxy.class));
logger.debug("Testing");
}

@Mock
LoggerManager loggerManager;

@Test
public void assertMockPolicyFails() throws Exception {
// This test fails
Reply all
Reply to author
Forward
0 new messages