Mocking statics

1,085 views
Skip to first unread message

MohanR

unread,
Jun 30, 2011, 7:04:15 AM6/30/11
to PowerMock
Does anyone have any idea why "java.lang.IllegalStateException:
incompatible return value type" is throws by PowerMock ?

@RunWith(PowerMockRunner.class)
@PrepareForTest(ManagementFactory.class)
public class MBeanServerTestCase extends MonitorTestCase{

private MBeanServer mBeanServerMock;


@Before
public void setUp() throws Exception {

mBeanServerMock =
PowerMock.createMock(MBeanServer.class);
mockStatic(ManagementFactory.class);

expect( ManagementFactory.getPlatformMBeanServer() ).

andReturn(mBeanServerMock); -> exception

replay( ManagementFactory.class );
mBeanServerMock =
ManagementFactory.getPlatformMBeanServer();
verify( ManagementFactory.class );
}


Thanks

Johan Haleby

unread,
Jun 30, 2011, 7:11:58 AM6/30/11
to powe...@googlegroups.com
Hi, 

I'm not quite sure about the exception, it seems like it's the wrong type. But I could be a consequence of the fact that ManagementFactory is a Java system class and must be mocked in a special way. Follow the documentation here on how mock system classes.

/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.


MohanR

unread,
Jun 30, 2011, 7:37:32 AM6/30/11
to PowerMock
The method is in javax.* and the instructions throws the same error.

@RunWith(PowerMockRunner.class)
@PrepareForTest(MBeanServerTestCase.class)
public class MBeanServerTestCase extends MonitorTestCase{

private MBeanServer mBeanServerMock;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Before
public void setUp() throws Exception {


mBeanServerMock = PowerMock.createMock(MBeanServer.class);
mockStatic(ManagementFactory.class);
expect( getPlatformMBeanServer() ).
andReturn(mBeanServerMock);

replayAll();
mBeanServerMock = getPlatformMBeanServer();
verifyAll();
}

public MBeanServer getPlatformMBeanServer() {
return ManagementFactory.getPlatformMBeanServer();
}

Johan Haleby

unread,
Jun 30, 2011, 7:41:03 AM6/30/11
to powe...@googlegroups.com
You probably need to replace "getPlatformMBeanServer()" with the actual call to ManagementFactory.getPlatformMBeanServer().

/Johan


--

MohanR

unread,
Jun 30, 2011, 7:57:00 AM6/30/11
to PowerMock
Really don't think that is the reason. 'mBeanServerMock' could be the
cause though but it is another mock.

Mohan

MohanR

unread,
Jul 1, 2011, 1:25:45 AM7/1/11
to PowerMock
What I meant to say was that I have now followed all the instructions.
There is a separate class like this. There is still an exception.

public class SystemClassAccessor {

public MBeanServer getPlatformMBeanServer() {
return ManagementFactory.getPlatformMBeanServer();
}

}


MohanR

unread,
Jul 1, 2011, 3:06:50 AM7/1/11
to PowerMock
Passed that stage with this code but any method on 'mBeanServerMock'
throws 'Unexpected method call'

@RunWith(PowerMockRunner.class)
@PrepareForTest(ManagementFactory.class)
@PowerMockIgnore(value = {"javax.management.*"})
public class MBeanServerTestCase extends MonitorTestCase{

private MBeanServer mBeanServerMock;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Before
public void setUp() throws Exception {


mBeanServerMock = PowerMock.createMock(MBeanServer.class);
mockStatic(ManagementFactory.class);
expect( ManagementFactory.getPlatformMBeanServer() ).
andReturn(mBeanServerMock);

replayAll();
mBeanServerMock = ManagementFactory.getPlatformMBeanServer();
verifyAll();
}

Johan Haleby

unread,
Jul 1, 2011, 3:21:02 AM7/1/11
to powe...@googlegroups.com
Hmm.. Could you try the test in a normal test method and not in a @Before method?

/Johan

       }

MohanR

unread,
Jul 4, 2011, 1:48:14 AM7/4/11
to PowerMock
Hi, Johan

This code solves it. But the number of hours spent on
this makes me squirm.

Thanks.



public void setUp() throws Exception {

mockStatic(ManagementFactory.class);
expect( ManagementFactory.getPlatformMBeanServer() ).

andReturn( EasyMock.createMock(MBeanServer.class) );

replayAll();
mBeanServerMock = ManagementFactory.getPlatformMBeanServer();
verifyAll();
}


@Test
public void testRegistration(){

try {
ObjectName on = new
ObjectName("com.test.bean:type=XMBean");
ObjectInstance oi = new ObjectInstance( on,

"com.test.bean.XMBean");

expect(mBeanServerMock.registerMBean(isA(XMBean.class),
eq(on))). --> Note this line too
andReturn(new ObjectInstance( on,
"com.test.bean.XMBean"));
}
}
Reply all
Reply to author
Forward
0 new messages