Migrate exception checking tests to use AssertJ

Migrate tests that use `@Test(expectedException=...)` or
`try...fail...catch` to use AssertJ's `assertThatException`
instead.
This commit is contained in:
Phillip Webb
2019-05-20 10:34:51 -07:00
parent fb26fc3f94
commit 02850f357f
561 changed files with 6592 additions and 10389 deletions

View File

@@ -29,6 +29,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
/**
@@ -66,11 +67,12 @@ public class InitializeDatabaseIntegrationTests {
assertCorrectSetup(context.getBean("dataSource", DataSource.class));
}
@Test(expected = BadSqlGrammarException.class)
@Test
public void testDisableCreateEmbeddedDatabase() throws Exception {
System.setProperty("ENABLED", "false");
context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-initialize-config.xml");
assertCorrectSetup(context.getBean("dataSource", DataSource.class));
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() ->
assertCorrectSetup(context.getBean("dataSource", DataSource.class)));
}
@Test

View File

@@ -28,6 +28,7 @@ import org.junit.Test;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.dao.TypeMismatchDataAccessException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.BDDMockito.given;
@@ -84,7 +85,7 @@ public class SingleColumnRowMapperTests {
assertEquals(timestamp.toLocalDateTime(), actualMyLocalDateTime.value);
}
@Test(expected = TypeMismatchDataAccessException.class) // SPR-16483
@Test // SPR-16483
public void doesNotUseConversionService() throws SQLException {
SingleColumnRowMapper<LocalDateTime> rowMapper =
SingleColumnRowMapper.newInstance(LocalDateTime.class, null);
@@ -96,8 +97,8 @@ public class SingleColumnRowMapperTests {
given(resultSet.getObject(1, LocalDateTime.class))
.willThrow(new SQLFeatureNotSupportedException());
given(resultSet.getTimestamp(1)).willReturn(new Timestamp(0));
rowMapper.mapRow(resultSet, 1);
assertThatExceptionOfType(TypeMismatchDataAccessException.class).isThrownBy(() ->
rowMapper.mapRow(resultSet, 1));
}

View File

@@ -24,6 +24,7 @@ import org.junit.Test;
import org.springframework.tests.sample.beans.TestBean;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -37,15 +38,17 @@ import static org.junit.Assert.assertTrue;
*/
public class BeanPropertySqlParameterSourceTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void withNullBeanPassedToCtor() {
new BeanPropertySqlParameterSource(null);
assertThatIllegalArgumentException().isThrownBy(() ->
new BeanPropertySqlParameterSource(null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void getValueWhereTheUnderlyingBeanHasNoSuchProperty() {
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new TestBean());
source.getValue("thisPropertyDoesNotExist");
assertThatIllegalArgumentException().isThrownBy(() ->
source.getValue("thisPropertyDoesNotExist"));
}
@Test
@@ -75,10 +78,11 @@ public class BeanPropertySqlParameterSourceTests {
assertFalse(source.hasValue("thisPropertyDoesNotExist"));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void getValueWhereTheUnderlyingBeanPropertyIsNotReadable() {
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(new NoReadableProperties());
source.getValue("noOp");
assertThatIllegalArgumentException().isThrownBy(() ->
source.getValue("noOp"));
}
@Test

View File

@@ -23,6 +23,7 @@ import org.junit.Test;
import org.springframework.jdbc.core.SqlParameterValue;
import org.springframework.jdbc.support.JdbcUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
/**
@@ -37,10 +38,11 @@ public class MapSqlParameterSourceTests {
new MapSqlParameterSource(null);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void getValueChokesIfParameterIsNotPresent() {
MapSqlParameterSource source = new MapSqlParameterSource();
source.getValue("pechorin was right!");
assertThatIllegalArgumentException().isThrownBy(() ->
source.getValue("pechorin was right!"));
}
@Test

View File

@@ -24,9 +24,9 @@ import org.junit.Test;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
/**
* @author Thomas Risberg
@@ -80,12 +80,8 @@ public class NamedParameterUtilsTests {
assertSame(5, NamedParameterUtils.buildValueArray("xxx :a :b :c xx :a :b", paramMap).length);
assertSame(5, NamedParameterUtils.buildValueArray("xxx :a :a :a xx :a :a", paramMap).length);
assertEquals("b", NamedParameterUtils.buildValueArray("xxx :a :b :c xx :a :b", paramMap)[4]);
try {
NamedParameterUtils.buildValueArray("xxx :a :b ?", paramMap);
fail("mixed named parameters and ? placeholders not detected");
}
catch (InvalidDataAccessApiUsageException expected) {
}
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).as("mixed named parameters and ? placeholders").isThrownBy(() ->
NamedParameterUtils.buildValueArray("xxx :a :b ?", paramMap));
}
@Test
@@ -118,10 +114,11 @@ public class NamedParameterUtilsTests {
.buildSqlParameterList(NamedParameterUtils.parseSqlStatement("xxx :a :b :c"), namedParams).get(2).getTypeName());
}
@Test(expected = InvalidDataAccessApiUsageException.class)
@Test
public void buildValueArrayWithMissingParameterValue() {
String sql = "select count(0) from foo where id = :id";
NamedParameterUtils.buildValueArray(sql, Collections.<String, Object>emptyMap());
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() ->
NamedParameterUtils.buildValueArray(sql, Collections.<String, Object>emptyMap()));
}
@Test

View File

@@ -35,7 +35,6 @@ import org.springframework.jdbc.support.lob.LobHandler;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -141,14 +140,9 @@ public class SqlLobValueTests {
SqlLobValue lob = new SqlLobValue("bla");
lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
try {
lob = new SqlLobValue("bla".getBytes());
lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
fail("IllegalArgumentException should have been thrown");
}
catch (IllegalArgumentException e) {
// expected
}
SqlLobValue lob2 = new SqlLobValue("bla".getBytes());
assertThatIllegalArgumentException().isThrownBy(() ->
lob2.setTypeValue(preparedStatement, 1, Types.CLOB, "test"));
lob = new SqlLobValue(new ByteArrayInputStream("bla".getBytes()), 3);
lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test");
@@ -167,16 +161,10 @@ public class SqlLobValueTests {
lob = new SqlLobValue(new ByteArrayInputStream("bla".getBytes()), 3);
lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
lob = new SqlLobValue(new InputStreamReader(new ByteArrayInputStream(
SqlLobValue lob3 = new SqlLobValue(new InputStreamReader(new ByteArrayInputStream(
"bla".getBytes())), 3);
try {
lob.setTypeValue(preparedStatement, 1, Types.BLOB, "test");
fail("IllegalArgumentException should have been thrown");
}
catch (IllegalArgumentException e) {
// expected
}
assertThatIllegalArgumentException().isThrownBy(() ->
lob3.setTypeValue(preparedStatement, 1, Types.BLOB, "test"));
}
@Test

View File

@@ -46,11 +46,11 @@ import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.atLeastOnce;
@@ -475,8 +475,9 @@ public class DataSourceJtaTransactionTests {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(TransactionException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
@@ -527,13 +528,7 @@ public class DataSourceJtaTransactionTests {
}
}
}
});
fail("Should have thrown TransactionException");
}
catch (TransactionException ex) {
// expected
}
}));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

View File

@@ -48,6 +48,8 @@ import org.springframework.transaction.support.TransactionSynchronizationAdapter
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@@ -225,31 +227,26 @@ public class DataSourceTransactionManagerTests {
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
final RuntimeException ex = new RuntimeException("Application exception");
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
assertTrue("Is new transaction", status.isNewTransaction());
Connection con = DataSourceUtils.getConnection(dsToUse);
if (createStatement) {
try {
con.createStatement();
}
catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
assertTrue("Is new transaction", status.isNewTransaction());
Connection con = DataSourceUtils.getConnection(dsToUse);
if (createStatement) {
try {
con.createStatement();
}
catch (SQLException ex) {
throw new UncategorizedSQLException("", "", ex);
}
}
throw ex;
}
throw ex;
}
});
fail("Should have thrown RuntimeException");
}
catch (RuntimeException ex2) {
// expected
assertTrue("Correct exception thrown", ex2.equals(ex));
}
}))
.isEqualTo(ex);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
@@ -384,31 +381,26 @@ public class DataSourceTransactionManagerTests {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
try {
final TransactionTemplate tt = new TransactionTemplate(tm);
final TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> {
final TransactionTemplate tt = new TransactionTemplate(tm);
final TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertFalse("Is not rollback-only", status.isRollbackOnly());
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
status.setRollbackOnly();
}
});
assertTrue("Is rollback-only", status.isRollbackOnly());
}
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertFalse("Is not rollback-only", status.isRollbackOnly());
tt2.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
status.setRollbackOnly();
}
});
assertTrue("Is rollback-only", status.isRollbackOnly());
}
});
});
fail("Should have thrown IllegalTransactionStateException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).rollback();
verify(con).close();
@@ -422,7 +414,7 @@ public class DataSourceTransactionManagerTests {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setReadOnly(true);
final TransactionTemplate tt2 = new TransactionTemplate(tm);
@@ -441,12 +433,7 @@ public class DataSourceTransactionManagerTests {
assertTrue("Is rollback-only", status.isRollbackOnly());
}
});
fail("Should have thrown IllegalTransactionStateException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).rollback();
@@ -546,9 +533,8 @@ public class DataSourceTransactionManagerTests {
final TestTransactionSynchronization synch =
new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_UNKNOWN);
try {
assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() -> {
assertTrue("Is new transaction", ts.isNewTransaction());
final TransactionTemplate tt = new TransactionTemplate(tm2);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@@ -571,12 +557,7 @@ public class DataSourceTransactionManagerTests {
});
tm.commit(ts);
fail("Should have thrown UnexpectedRollbackException");
}
catch (UnexpectedRollbackException ex) {
// expected
}
});
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertFalse(synch.beforeCommitCalled);
@@ -692,7 +673,7 @@ public class DataSourceTransactionManagerTests {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds2));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
@@ -707,12 +688,7 @@ public class DataSourceTransactionManagerTests {
}
});
}
});
fail("Should have thrown CannotCreateTransactionException");
}
catch (CannotCreateTransactionException ex) {
assertSame(failure, ex.getCause());
}
})).withCause(failure);
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds2));
@@ -763,7 +739,7 @@ public class DataSourceTransactionManagerTests {
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
@@ -777,11 +753,7 @@ public class DataSourceTransactionManagerTests {
});
fail("Should have thrown IllegalTransactionStateException");
}
});
}
catch (IllegalTransactionStateException ex) {
// expected
}
}));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).rollback();
@@ -1143,18 +1115,13 @@ public class DataSourceTransactionManagerTests {
willThrow(new SQLException("Cannot begin")).given(con).getAutoCommit();
TransactionTemplate tt = new TransactionTemplate(tm);
try {
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown CannotCreateTransactionException");
}
catch (CannotCreateTransactionException ex) {
// expected
}
}));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).close();
@@ -1165,18 +1132,13 @@ public class DataSourceTransactionManagerTests {
willThrow(new SQLException("Cannot commit")).given(con).commit();
TransactionTemplate tt = new TransactionTemplate(tm);
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
}));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).close();
@@ -1188,18 +1150,13 @@ public class DataSourceTransactionManagerTests {
tm.setRollbackOnCommitFailure(true);
TransactionTemplate tt = new TransactionTemplate(tm);
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
}));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
verify(con).rollback();
@@ -1212,18 +1169,13 @@ public class DataSourceTransactionManagerTests {
willThrow(new SQLException("Cannot rollback")).given(con).rollback();
TransactionTemplate tt = new TransactionTemplate(tm);
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
status.setRollbackOnly();
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
}));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
InOrder ordered = inOrder(con);
@@ -1398,7 +1350,7 @@ public class DataSourceTransactionManagerTests {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Is new transaction", status.isNewTransaction());
assertTrue("Isn't nested transaction", !status.hasSavepoint());
try {
assertThatIllegalStateException().isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
@@ -1418,12 +1370,7 @@ public class DataSourceTransactionManagerTests {
}
});
}
});
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}));
assertTrue("Is new transaction", status.isNewTransaction());
assertTrue("Isn't nested transaction", !status.hasSavepoint());
}
@@ -1456,7 +1403,7 @@ public class DataSourceTransactionManagerTests {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertTrue("Is new transaction", status.isNewTransaction());
assertTrue("Isn't nested transaction", !status.hasSavepoint());
try {
assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
@@ -1476,12 +1423,7 @@ public class DataSourceTransactionManagerTests {
}
});
}
});
fail("Should have thrown UnexpectedRollbackException");
}
catch (UnexpectedRollbackException ex) {
// expected
}
}));
assertTrue("Is new transaction", status.isNewTransaction());
assertTrue("Isn't nested transaction", !status.hasSavepoint());
}

View File

@@ -21,9 +21,9 @@ import java.util.Properties;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
/**
@@ -139,14 +139,9 @@ public class DriverManagerDataSourceTests {
public void testInvalidClassName() throws Exception {
String bogusClassName = "foobar";
DriverManagerDataSource ds = new DriverManagerDataSource();
try {
ds.setDriverClassName(bogusClassName);
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// OK
assertTrue(ex.getCause() instanceof ClassNotFoundException);
}
assertThatIllegalStateException().isThrownBy(() ->
ds.setDriverClassName(bogusClassName))
.withCauseInstanceOf(ClassNotFoundException.class);
}
}

View File

@@ -23,8 +23,8 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.CannotReadScriptException;
import org.springframework.jdbc.datasource.init.ScriptStatementFailedException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.DERBY;
import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.H2;
@@ -54,9 +54,10 @@ public class EmbeddedDatabaseBuilderTests {
});
}
@Test(expected = CannotReadScriptException.class)
@Test
public void addScriptWithBogusFileName() {
new EmbeddedDatabaseBuilder().addScript("bogus.sql").build();
assertThatExceptionOfType(CannotReadScriptException.class).isThrownBy(
new EmbeddedDatabaseBuilder().addScript("bogus.sql")::build);
}
@Test
@@ -165,17 +166,11 @@ public class EmbeddedDatabaseBuilderTests {
@Test
public void createSameSchemaTwiceWithoutUniqueDbNames() throws Exception {
EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
.addScripts("db-schema-without-dropping.sql").build();
EmbeddedDatabase db1 = new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))
.addScripts("db-schema-without-dropping.sql").build();
try {
new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass()))//
.addScripts("db-schema-without-dropping.sql").build();
fail("Should have thrown a ScriptStatementFailedException");
}
catch (ScriptStatementFailedException e) {
// expected
assertThatExceptionOfType(ScriptStatementFailedException.class).isThrownBy(() ->
new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(getClass())).addScripts("db-schema-without-dropping.sql").build());
}
finally {
db1.shutdown();

View File

@@ -21,6 +21,7 @@ import org.mockito.Mockito;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
/**
@@ -37,14 +38,16 @@ public class ResourceDatabasePopulatorTests {
private static final Resource script3 = Mockito.mock(Resource.class);
@Test(expected = IllegalArgumentException.class)
@Test
public void constructWithNullResource() {
new ResourceDatabasePopulator((Resource) null);
assertThatIllegalArgumentException().isThrownBy(() ->
new ResourceDatabasePopulator((Resource) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void constructWithNullResourceArray() {
new ResourceDatabasePopulator((Resource[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
new ResourceDatabasePopulator((Resource[]) null));
}
@Test
@@ -68,28 +71,32 @@ public class ResourceDatabasePopulatorTests {
assertEquals(3, databasePopulator.scripts.size());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void addScriptsWithNullResource() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScripts((Resource) null);
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.addScripts((Resource) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void addScriptsWithNullResourceArray() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScripts((Resource[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.addScripts((Resource[]) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setScriptsWithNullResource() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.setScripts((Resource) null);
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.setScripts((Resource) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void setScriptsWithNullResourceArray() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.setScripts((Resource[]) null);
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.setScripts((Resource[]) null));
}
@Test

View File

@@ -23,9 +23,10 @@ import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -62,18 +63,16 @@ public class BeanFactoryDataSourceLookupTests {
new BeanNotOfRequiredTypeException(DATASOURCE_BEAN_NAME,
DataSource.class, String.class));
try {
BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(beanFactory);
lookup.getDataSource(DATASOURCE_BEAN_NAME);
fail("should have thrown DataSourceLookupFailureException");
}
catch (DataSourceLookupFailureException ex) { /* expected */ }
BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(beanFactory);
assertThatExceptionOfType(DataSourceLookupFailureException.class).isThrownBy(() ->
lookup.getDataSource(DATASOURCE_BEAN_NAME));
}
@Test(expected = IllegalStateException.class)
@Test
public void testLookupWhereBeanFactoryHasNotBeenSupplied() throws Exception {
BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup();
lookup.getDataSource(DATASOURCE_BEAN_NAME);
assertThatIllegalStateException().isThrownBy(() ->
lookup.getDataSource(DATASOURCE_BEAN_NAME));
}
}

View File

@@ -21,6 +21,7 @@ import javax.sql.DataSource;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
@@ -48,7 +49,7 @@ public class JndiDataSourceLookupTests {
assertSame(expectedDataSource, dataSource);
}
@Test(expected = DataSourceLookupFailureException.class)
@Test
public void testNoDataSourceAtJndiLocation() throws Exception {
JndiDataSourceLookup lookup = new JndiDataSourceLookup() {
@Override
@@ -57,7 +58,8 @@ public class JndiDataSourceLookupTests {
throw new NamingException();
}
};
lookup.getDataSource(DATA_SOURCE_NAME);
assertThatExceptionOfType(DataSourceLookupFailureException.class).isThrownBy(() ->
lookup.getDataSource(DATA_SOURCE_NAME));
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.UncategorizedSQLException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
@@ -46,9 +47,10 @@ public class SQLStateSQLExceptionTranslatorTests {
private static final String SQL = "select count(0) from t_sheep where over_fence = ... yawn... 1";
@Test(expected = IllegalArgumentException.class)
@Test
public void testTranslateNullException() throws Exception {
new SQLStateSQLExceptionTranslator().translate("", "", null);
assertThatIllegalArgumentException().isThrownBy(() ->
new SQLStateSQLExceptionTranslator().translate("", "", null));
}
@Test

View File

@@ -30,8 +30,8 @@ import org.junit.Test;
import org.springframework.jdbc.InvalidResultSetAccessException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -215,13 +215,9 @@ public class ResultSetWrappingRowSetTests {
given(rsetMethod.invoke(resultSet, arg)).willReturn(ret).willThrow(new SQLException("test"));
}
rowsetMethod.invoke(rowSet, arg);
try {
rowsetMethod.invoke(rowSet, arg);
fail("InvalidResultSetAccessException should have been thrown");
}
catch (InvocationTargetException ex) {
assertEquals(InvalidResultSetAccessException.class, ex.getTargetException().getClass());
}
assertThatExceptionOfType(InvocationTargetException.class).isThrownBy(() ->
rowsetMethod.invoke(rowSet, arg)).
satisfies(ex -> assertThat(ex.getTargetException()).isExactlyInstanceOf(InvalidResultSetAccessException.class));
}
}