ListenerRepo { public Listener insert(Listener listener, Connection connection) throws DataAccessException { try ( PreparedStatement stmt = connection .prepareStatement(INSERT_LISTENER_QUERY, Statement.RETURN_GENERATED_KEYS) ) { if (listener.getId() == 0l) { stmt.setObject(1, null); } else { stmt.setObject(1, listener.getId()); } stmt.setObject(2, listener.getTableName()); stmt.setObject(3, listener.getWaitTimeout()); stmt.executeUpdate(); ResultSet rs = stmt.getGeneratedKeys(); rs.next(); long genId = rs.getLong("GENERATED_KEY"); return findById(genId, connection); } catch (SQLException e) { throw new DataAccessException("Couldn't insert listener", e); } }  Listener insert(Listener listener, Connection connection); void refreshLastCheck(Listener listener, Connection connection); void remove(Listener listener, Connection connection); void update(Listener listener, Connection connection); Listener findById(long id, Connection connection); List<Listener> findByTableName(String tableName, Connection connection);  }
@Test(groups = INTEGRATION) public void testInsertingNotPersistedListenerDoesNotUpdateIdButReturnsPersistedListener() { Listener listener = new Listener(TABLE_NAME, WAIT_IN_SEC); Listener persistedListener = repo.insert(listener, connection); assertEquals(listener.getId(), 0L); assertNotEquals(persistedListener.getId(), 0L); }
@Test(groups = SMALL, expectedExceptions = DataAccessException.class) public void testInsertThrowsDataAccessExceptionWhenSqlExceptionOccurs() throws SQLException { Connection mockedConnection = mock(Connection.class); when(mockedConnection.prepareStatement(anyString(), anyInt())).thenThrow(new SQLException()); MysqlConnectionProvider mockedProvider = mock(MysqlConnectionProvider.class); when(mockedProvider.getConnection()).thenReturn(mockedConnection); ListenerRepo mockedRepo = new ListenerRepo(); mockedRepo.insert(mock(Listener.class), mockedConnection); }