@Test public void shouldAddSecondParameterWithIndexTwoWhenTwoBooleanParametersGiven() throws SQLException { sqlBuilder.prepareStatement("query").withParameter(false).withParameter(true); verify(preparedStatementMock, atLeastOnce()).setBoolean(1, false); verify(preparedStatementMock, atLeastOnce()).setBoolean(2, true); }
@Test public void shouldAddSecondParameterWithIndexTwoWhenTwoIntegerParametersGiven() throws SQLException { sqlBuilder.prepareStatement("query").withParameter(1).withParameter(2); verify(preparedStatementMock, atLeastOnce()).setInt(1, 1); verify(preparedStatementMock, atLeastOnce()).setInt(2, 2); }
@Test public void shouldAddSecondParameterWithIndexTwoWhenTwoStringParametersGiven() throws SQLException { sqlBuilder.prepareStatement("query").withParameter("test").withParameter("second"); verify(preparedStatementMock, atLeastOnce()).setString(1, "test"); verify(preparedStatementMock, atLeastOnce()).setString(2, "second"); }
@Test public void shouldResetParameterIndexWhenContinueWithOnOpenStatementGiven() throws SQLException { sqlBuilder.prepareStatement("select 1 from dual"); sqlBuilder.withParameter(true); sqlBuilder.withParameter(false); sqlBuilder.continueWith().prepareStatement("select 2 from dual"); sqlBuilder.withParameter("test"); verify(preparedStatementMock, times(1)).setBoolean(1, true); verify(preparedStatementMock, times(1)).setBoolean(2, false); verify(preparedStatementMock, times(1)).setString(1, "test"); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenParameterOnClosedBuilderGiven() throws SQLException { sqlBuilder.close(); sqlBuilder.prepareStatement("bla"); }
@Test public void shouldAddParameterWhenStringParameterGiven() throws SQLException { sqlBuilder.prepareStatement("query").withParameter("test"); verify(preparedStatementMock, atLeastOnce()).setString(1, "test"); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenClosedSqlBuilderGiven() throws SQLException { SqlBuilder sqlBuilder = new SqlBuilder(dataSourceMock); sqlBuilder.close(); sqlBuilder.prepareStatement("some query"); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenNoSqlQueryGiven() throws SQLException { sqlBuilder.prepareStatement(null); }
@Test public void shouldPrepareStatementWhenSqlQueryGiven() throws SQLException { sqlBuilder.prepareStatement("select 1 from dual"); verify(connectionMock, atLeastOnce()).prepareStatement("select 1 from dual"); }
@Test public void shouldReturnResultSetWhenUpdateOnOpenStatementGiven() throws SQLException { sqlBuilder.prepareStatement("select 1 from dual"); sqlBuilder.update(); verify(preparedStatementMock, times(1)).executeUpdate(); }
@Test(expected = JdbcException.class) public void shouldThrowRuntimeExceptionWhenSqlExceptionGiven() throws SQLException { Mockito.doThrow(new SQLException("something bad happened")).when(connectionMock).close(); sqlBuilder.prepareStatement("select 1 from dual"); sqlBuilder.close(); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenUpdateOnClosedStatementGiven() throws SQLException { sqlBuilder.prepareStatement("select 1 from dual"); sqlBuilder.close(); sqlBuilder.update(); }
@Test public void shouldCloseStatementWhenContinueWithOnOpenStatementGiven() throws SQLException { sqlBuilder.prepareStatement("select 1 from dual"); sqlBuilder.continueWith(); verify(preparedStatementMock, times(1)).close(); }
@Test public void shouldEnableAutoCommitWhenWithoutTransactionGiven() throws SQLException { sqlBuilder.prepareStatement("select 1 from dual").withoutTransaction(); verify(connectionMock, atLeastOnce()).setAutoCommit(true); }