### Question:
NcTools { static Number getNumericAttributeValue(Variable variable, String name) { final Attribute attribute = variable.findAttribute(name); if (attribute != null) { return attribute.getNumericValue(); } else { return null; } }  static Variable getVariable(NetcdfFile netcdfFile, String variableName); static boolean hasVariable(NetcdfFile netcdfFile, String variableName); static Grid readGrid(NetcdfFile netcdfFile, Variable variable, GridDef expectedGridDef, int z); static Grid readGrid(NetcdfFile netcdfFile, String variableName, GridDef expectedGridDef, int z); static Grid readGrid(NetcdfFile netcdfFile, String variableName, GridDef expectedGridDef);  }### Answer:
@Test public void testGetNumericAttributeValue() { final String attributeName = "an_attribute"; final Variable variable = mock(Variable.class); final Attribute attribute = mock(Attribute.class); when(attribute.getNumericValue()).thenReturn(108); when(variable.findAttribute(attributeName)).thenReturn(attribute); final Number value = NcTools.getNumericAttributeValue(variable, attributeName); assertEquals(108, value.longValue()); verify(variable, times(1)).findAttribute(attributeName); verify(attribute, times(1)).getNumericValue(); verifyNoMoreInteractions(variable); verifyNoMoreInteractions(attribute); }
@Test public void testGetNumericAttributeValue_attributeNotPresent() { final Variable variable = mock(Variable.class); when(variable.findAttribute(anyString())).thenReturn(null); final Number value = NcTools.getNumericAttributeValue(variable, "is_not_there"); assertNull(value); verify(variable, times(1)).findAttribute(anyString()); verifyNoMoreInteractions(variable); }
@Test public void testGetNumericAttributeValue_withDefault() { final String attributeName = "the_attribute"; final Variable variable = mock(Variable.class); final Attribute attribute = mock(Attribute.class); when(attribute.getNumericValue()).thenReturn(109.7); when(variable.findAttribute(attributeName)).thenReturn(attribute); final Number value = NcTools.getNumericAttributeValue(variable, attributeName, 22.8); assertEquals(109.7, value.doubleValue(), 1e-8); verify(variable, times(1)).findAttribute(attributeName); verify(attribute, times(1)).getNumericValue(); verifyNoMoreInteractions(variable); verifyNoMoreInteractions(attribute); }
@Test public void testGetNumericAttributeValue_withDefault_defaultValueReturned() { final Variable variable = mock(Variable.class); when(variable.findAttribute(anyString())).thenReturn(null); final Number value = NcTools.getNumericAttributeValue(variable, "is_not_there", 29.66); assertNotNull(value); assertEquals(29.66, value.doubleValue(), 1e-8); verify(variable, times(1)).findAttribute(anyString()); verifyNoMoreInteractions(variable); }