Mock tests for JMX
July 28, 2011 Leave a comment
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.