Source:
<code>private MBeanServer getMockMBeanServer() throws Exception { MBeanServer mbeanServer = mock(MBeanServer.class); Set<ObjectName> names = new HashSet<>(); names.add(new ObjectName("test1:type=test2")); when(mbeanServer.queryNames(any(ObjectName.class), any(QueryExp.class))).thenReturn(names); MBeanInfo mbeanInfo = mock(MBeanInfo.class); when(mbeanInfo.getClassName()).thenReturn("testClassName"); String[] attributeNames = new String[] {"intAttr", "nanAttr", "strAttr", "boolAttr"}; MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributeNames.length]; for (int i = 0; i < attributeInfos.length; i++) { attributeInfos[i] = new MBeanAttributeInfo(attributeNames[i], null, null, true, false, false); } when(mbeanInfo.getAttributes()).thenReturn(attributeInfos); when(mbeanServer.getMBeanInfo(any(ObjectName.class))).thenReturn(mbeanInfo); <START> when(mbeanServer.getAttribute(any(ObjectName.class), eq("intAttr"))).thenReturn(3); <END> when(mbeanServer.getAttribute(any(ObjectName.class), eq("nanAttr"))).thenReturn(Double.NaN); when(mbeanServer.getAttribute(any(ObjectName.class), eq("strAttr"))).thenReturn("aString"); when(mbeanServer.getAttribute(any(ObjectName.class), eq("boolAttr"))).thenReturn(true); return mbeanServer; }</code><technical_language>Another test infinity coverage good cover code is supposed print. Basically do this: private MBeanServer getMockMBeanServer(Number number) throws Exception { .... ... when(mbeanServer.getAttribute(any(ObjectName.class), eq("intAttr"))).thenReturn(number); } private String getExpectedJSON(String str) { ... ... pw.println(" \"intAttr\": "+str+","); } call jsonWriter.write(getMockMBeanServer(10.0/0.0), null, null, false); assertEquals(mapper.readTree(getExpectedJSON("\"Infinity\"")), mapper.readTree(stringWriter.toString())); jsonWriter.write(getMockMBeanServer(3), null, null, false); assertEquals(mapper.readTree(getExpectedJSON("3")), mapper.readTree(stringWriter.toString()));</technical_language>

Target:
private MBeanServer getMockMBeanServer() throws Exception { MBeanServer mbeanServer = mock(MBeanServer.class); Set<ObjectName> names = new HashSet<>(); names.add(new ObjectName("test1:type=test2")); when(mbeanServer.queryNames(any(ObjectName.class), any(QueryExp.class))).thenReturn(names); MBeanInfo mbeanInfo = mock(MBeanInfo.class); when(mbeanInfo.getClassName()).thenReturn("testClassName"); String[] attributeNames = new String[] {"intAttr", "nanAttr", "infinityAttr", "strAttr", "boolAttr"}; MBeanAttributeInfo[] attributeInfos = new MBeanAttributeInfo[attributeNames.length]; for (int i = 0; i < attributeInfos.length; i++) { attributeInfos[i] = new MBeanAttributeInfo(attributeNames[i], null, null, true, false, false); } when(mbeanInfo.getAttributes()).thenReturn(attributeInfos); when(mbeanServer.getMBeanInfo(any(ObjectName.class))).thenReturn(mbeanInfo); when(mbeanServer.getAttribute(any(ObjectName.class), eq("intAttr"))).thenReturn(3); when(mbeanServer.getAttribute(any(ObjectName.class), eq("nanAttr"))).thenReturn(Double.NaN); when(mbeanServer.getAttribute(any(ObjectName.class), eq("infinityAttr"))). thenReturn(Double.POSITIVE_INFINITY); when(mbeanServer.getAttribute(any(ObjectName.class), eq("strAttr"))).thenReturn("aString"); when(mbeanServer.getAttribute(any(ObjectName.class), eq("boolAttr"))).thenReturn(true); return mbeanServer; }