### Question:
NcTools { public static Variable getVariable(NetcdfFile netcdfFile, String variableName) throws IOException { final Variable variable = netcdfFile.findVariable(variableName); if (variable == null) { throw new IOException(String.format("Missing variable '%s' in file '%s'.", variableName, netcdfFile.getLocation())); } return variable; }  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 testGetVariable() throws IOException { final Variable mockVariable = mock(Variable.class); final String variableName = "blablupp"; when(netcdfFile.findVariable(variableName)).thenReturn(mockVariable); final Variable variable = NcTools.getVariable(netcdfFile, variableName); assertNotNull(variable); verify(netcdfFile, times(1)).findVariable(variableName); verifyNoMoreInteractions(netcdfFile); }
@Test public void testGetVariable_invalidVariableName() throws IOException { final String variableName = "schnickschnack"; when(netcdfFile.findVariable(variableName)).thenReturn(null); try { NcTools.getVariable(netcdfFile, variableName); fail("IOException expected"); } catch (IOException expected) { } verify(netcdfFile, times(1)).findVariable(variableName); verify(netcdfFile, times(1)).getLocation(); verifyNoMoreInteractions(netcdfFile); }