### Question:
JpaStorage implements Storage { @Override public Sensor getSensorWithTransaction(String sensorName) { try { persistenceManager.transaction(); final Sensor sensor = getSensor(sensorName); persistenceManager.commit(); return sensor; } catch (Exception e) { persistenceManager.rollback(); throw new ToolException("Database error", e, ToolException.TOOL_DB_ERROR); } } JpaStorage(PersistenceManager persistenceManager); @Override DataFile getDatafile(int id); @Override final DataFile getDatafile(final String path); @Override DataFile getDatafileWithTransaction(String path); void store(DataFile dataFile); @Override int storeWithTransaction(DataFile dataFile); @Override Observation getObservation(int id); @Override RelatedObservation getRelatedObservation(int id); @Override ReferenceObservation getReferenceObservation(int id); @Override final Sensor getSensor(final String sensorName); @Override Sensor getSensorWithTransaction(String sensorName); @Override void storeWithTransaction(Sensor sensor); @Override List<RelatedObservation> getRelatedObservations(String sensorName, Date startDate, Date stopDate); @Override void store(InsituObservation insituObservation);  }### Answer:
@Test public void testGetSensorWithTransaction() { final String sql = "select s from Sensor s where s.name = ?1"; final String sensorName = "blabla"; final Sensor sensor = new SensorBuilder().name(sensorName).build(); when(persistenceManager.pick(sql, sensorName)).thenReturn(sensor); final Sensor toolStorageSensor = jpaStorage.getSensorWithTransaction(sensorName); assertNotNull(toolStorageSensor); assertEquals(sensorName, toolStorageSensor.getName()); verify(persistenceManager, times(1)).transaction(); verify(persistenceManager, times(1)).pick(sql, sensorName); verify(persistenceManager, times(1)).commit(); verifyNoMoreInteractions(persistenceManager); }
@Test public void testGetSensorWithTransaction_failure() { final String sql = "select s from Sensor s where s.name = ?1"; final String sensorName = "blabla"; doThrow(new PersistenceException(null, null, null, true)).when(persistenceManager).pick(sql, sensorName); try { jpaStorage.getSensorWithTransaction(sensorName); fail("ToolException expected"); } catch (ToolException expected) { } verify(persistenceManager, times(1)).transaction(); verify(persistenceManager, times(1)).pick(sql, sensorName); verify(persistenceManager, times(1)).rollback(); verifyNoMoreInteractions(persistenceManager); }