@Test public void testUnregisterMBean() throws Exception { RoundRobinSchedulerStats stats = EasyMock.createMockBuilder(RoundRobinSchedulerStats.class) .withConstructor() .addMockedMethod("getMBeanServer") .createStrictMock(); // do nothing EasyMock.replay(stats); stats.unregisterMBean(); EasyMock.verify(stats); // now work stats.m_mbeanName = stats.createObjectName(); EasyMock.reset(stats); MBeanServer mbs = EasyMock.createStrictMock(MBeanServer.class); EasyMock.expect(stats.getMBeanServer()).andReturn(mbs); mbs.unregisterMBean(stats.m_mbeanName); EasyMock.expectLastCall(); EasyMock.replay(stats, mbs); stats.unregisterMBean(); EasyMock.verify(stats, mbs); assertNull("Object name should be null.", stats.m_mbeanName); // with exception that is just logged stats.m_mbeanName = stats.createObjectName(); EasyMock.reset(stats, mbs); EasyMock.expect(stats.getMBeanServer()).andReturn(mbs); mbs.unregisterMBean(stats.m_mbeanName); EasyMock.expectLastCall().andThrow(new MBeanRegistrationException(new Exception())); EasyMock.replay(stats, mbs); stats.unregisterMBean(); EasyMock.verify(stats, mbs); assertNull("Object name should be null.", stats.m_mbeanName); }
@Test public void testRegisterMBean() throws Exception { RoundRobinSchedulerStats stats = EasyMock.createMockBuilder(RoundRobinSchedulerStats.class) .withConstructor() .addMockedMethod("getMBeanServer") .addMockedMethod("createObjectName") .createStrictMock(); // do nothing stats.setUseJMX(false); EasyMock.replay(stats); stats.registerMBean(); EasyMock.verify(stats); assertNull("Object name should still be null.", stats.m_mbeanName); // now work EasyMock.reset(stats); stats.setUseJMX(true); MBeanServer mbs = EasyMock.createStrictMock(MBeanServer.class); ObjectName objectName = EasyMock.createStrictMock(ObjectName.class); EasyMock.expect(stats.getMBeanServer()).andReturn(mbs); EasyMock.expect(stats.createObjectName()).andReturn(objectName); EasyMock.expect(mbs.registerMBean(stats, objectName)).andReturn(null); EasyMock.replay(stats, mbs, objectName); stats.registerMBean(); EasyMock.verify(stats, mbs, objectName); assertEquals("Object name should not be null.", objectName, stats.m_mbeanName); // with exception that is just logged EasyMock.reset(stats, mbs, objectName); stats.m_mbeanName = null; EasyMock.expect(stats.getMBeanServer()).andReturn(mbs); EasyMock.expect(stats.createObjectName()).andReturn(objectName); EasyMock.expect(mbs.registerMBean(stats, objectName)) .andThrow(new NotCompliantMBeanException("blah")); EasyMock.replay(stats, mbs, objectName); stats.registerMBean(); EasyMock.verify(stats, mbs, objectName); assertNull("Object name should still be null.", stats.m_mbeanName); }