Mock tests for JMX

There is not much scope for this blog if code is posted without explanation but I can explain only what I understand. There is magic going on behind this code.

I had to write mocks for some JMX classes for a project that I could not convince the management to use. JMX is a useful toolkit to monitor complex pipelines of processes and is integral to DevOps. So firms that want to leverage this idea should think differently.

Does it seem logical ?

This argument fell flat on its face.

This code mocks “ManagementFactory.getPlatformMBeanServer()” and in doing so I exercised EasyMock. It is not easy to mock statics but these EasyMock annotations help. So I am able to set the expectations and make the Platform Bean Server return a mock of the MBeanServer. This is very useful because now I can register MBeans and write more mock tests.

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


    private MBeanServer mBeanServerMock;

    private final String CLASSNAME = this.getClass().getName();

    @Before
    public void setUp() throws Exception {
        mockStatic(ManagementFactory.class);
        expect( ManagementFactory.getPlatformMBeanServer() ).
                                    andReturn( EasyMock.createMock(MBeanServer.class) );

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

    }
}

Mock testing is another useful toolkit in the arsenal of a hardy developer but that is another idea that fell flat on its face.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: