Use AssertJ’s exception assertions rather than fail
Closes gh-15761
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,7 +31,7 @@ import org.springframework.context.annotation.ImportSelector;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link OnBeanCondition} when deduction of the bean's type fails
|
||||
@@ -44,21 +44,20 @@ public class OnBeanConditionTypeDeductionFailureTests {
|
||||
|
||||
@Test
|
||||
public void conditionalOnMissingBeanWithDeducedTypeThatIsPartiallyMissingFromClassPath() {
|
||||
try {
|
||||
new AnnotationConfigApplicationContext(ImportingConfiguration.class).close();
|
||||
fail("Context refresh was successful");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Throwable beanTypeDeductionException = findNestedCause(ex,
|
||||
BeanTypeDeductionException.class);
|
||||
assertThat(beanTypeDeductionException)
|
||||
.hasMessage("Failed to deduce bean type for "
|
||||
+ OnMissingBeanConfiguration.class.getName()
|
||||
+ ".objectMapper");
|
||||
assertThat(findNestedCause(beanTypeDeductionException,
|
||||
NoClassDefFoundError.class)).isNotNull();
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(
|
||||
() -> new AnnotationConfigApplicationContext(ImportingConfiguration.class)
|
||||
.close())
|
||||
.satisfies((ex) -> {
|
||||
Throwable beanTypeDeductionException = findNestedCause(ex,
|
||||
BeanTypeDeductionException.class);
|
||||
assertThat(beanTypeDeductionException)
|
||||
.hasMessage("Failed to deduce bean type for "
|
||||
+ OnMissingBeanConfiguration.class.getName()
|
||||
+ ".objectMapper");
|
||||
assertThat(findNestedCause(beanTypeDeductionException,
|
||||
NoClassDefFoundError.class)).isNotNull();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Throwable findNestedCause(Throwable ex, Class<? extends Throwable> target) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -47,7 +47,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataSourceInitializerInvoker}.
|
||||
@@ -178,16 +178,13 @@ public class DataSourceInitializerInvokerTests {
|
||||
|
||||
private void assertDataSourceNotInitialized(DataSource dataSource) {
|
||||
JdbcOperations template = new JdbcTemplate(dataSource);
|
||||
try {
|
||||
template.queryForObject("SELECT COUNT(*) from BAR", Integer.class);
|
||||
fail("Query should have failed as BAR table does not exist");
|
||||
}
|
||||
catch (BadSqlGrammarException ex) {
|
||||
SQLException sqlException = ex.getSQLException();
|
||||
int expectedCode = -5501; // user lacks privilege or object not
|
||||
// found
|
||||
assertThat(sqlException.getErrorCode()).isEqualTo(expectedCode);
|
||||
}
|
||||
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(
|
||||
() -> template.queryForObject("SELECT COUNT(*) from BAR", Integer.class))
|
||||
.satisfies((ex) -> {
|
||||
SQLException sqlException = ex.getSQLException();
|
||||
int expectedCode = -5501; // user lacks privilege or object not found
|
||||
assertThat(sqlException.getErrorCode()).isEqualTo(expectedCode);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -52,7 +52,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link JooqAutoConfiguration}.
|
||||
@@ -90,15 +90,10 @@ public class JooqAutoConfigurationTests {
|
||||
"insert into jooqtest (name) values ('foo');"));
|
||||
dsl.transaction(new AssertFetch(dsl,
|
||||
"select count(*) as total from jooqtest;", "1"));
|
||||
try {
|
||||
dsl.transaction(new ExecuteSql(dsl,
|
||||
"insert into jooqtest (name) values ('bar');",
|
||||
"insert into jooqtest (name) values ('foo');"));
|
||||
fail("An DataIntegrityViolationException should have been thrown.");
|
||||
}
|
||||
catch (DataIntegrityViolationException ex) {
|
||||
// Ignore
|
||||
}
|
||||
assertThatExceptionOfType(DataIntegrityViolationException.class)
|
||||
.isThrownBy(() -> dsl.transaction(new ExecuteSql(dsl,
|
||||
"insert into jooqtest (name) values ('bar');",
|
||||
"insert into jooqtest (name) values ('foo');")));
|
||||
dsl.transaction(new AssertFetch(dsl,
|
||||
"select count(*) as total from jooqtest;", "2"));
|
||||
});
|
||||
@@ -120,19 +115,14 @@ public class JooqAutoConfigurationTests {
|
||||
"insert into jooqtest_tx (name) values ('foo');"));
|
||||
dsl.transaction(new AssertFetch(dsl,
|
||||
"select count(*) as total from jooqtest_tx;", "1"));
|
||||
try {
|
||||
dsl.transaction(new ExecuteSql(dsl,
|
||||
"insert into jooqtest (name) values ('bar');",
|
||||
"insert into jooqtest (name) values ('foo');"));
|
||||
fail("A DataIntegrityViolationException should have been thrown.");
|
||||
}
|
||||
catch (DataIntegrityViolationException ex) {
|
||||
// Ignore
|
||||
}
|
||||
assertThatExceptionOfType(DataIntegrityViolationException.class)
|
||||
.isThrownBy(() -> dsl.transaction(new ExecuteSql(dsl,
|
||||
"insert into jooqtest (name) values ('bar');",
|
||||
"insert into jooqtest (name) values ('foo');")));
|
||||
dsl.transaction(new AssertFetch(dsl,
|
||||
"select count(*) as total from jooqtest_tx;", "1"));
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -42,8 +42,8 @@ import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConditionEvaluationReportLoggingListener}.
|
||||
@@ -76,15 +76,10 @@ public class ConditionEvaluationReportLoggingListenerTests {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
this.initializer.initialize(context);
|
||||
context.register(ErrorConfig.class);
|
||||
try {
|
||||
context.refresh();
|
||||
fail("Did not error");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
withDebugLogging(
|
||||
() -> this.initializer.onApplicationEvent(new ApplicationFailedEvent(
|
||||
new SpringApplication(), new String[0], context, ex)));
|
||||
}
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(context::refresh).satisfies(
|
||||
(ex) -> withDebugLogging(() -> this.initializer.onApplicationEvent(
|
||||
new ApplicationFailedEvent(new SpringApplication(), new String[0],
|
||||
context, ex))));
|
||||
assertThat(this.outputCapture.toString())
|
||||
.contains("CONDITIONS EVALUATION REPORT");
|
||||
}
|
||||
@@ -94,14 +89,10 @@ public class ConditionEvaluationReportLoggingListenerTests {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
this.initializer.initialize(context);
|
||||
context.register(ErrorConfig.class);
|
||||
try {
|
||||
context.refresh();
|
||||
fail("Did not error");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
this.initializer.onApplicationEvent(new ApplicationFailedEvent(
|
||||
new SpringApplication(), new String[0], context, ex));
|
||||
}
|
||||
assertThatExceptionOfType(Exception.class).isThrownBy(context::refresh)
|
||||
.satisfies((ex) -> this.initializer.onApplicationEvent(
|
||||
new ApplicationFailedEvent(new SpringApplication(), new String[0],
|
||||
context, ex)));
|
||||
assertThat(this.outputCapture.toString()).contains("Error starting"
|
||||
+ " ApplicationContext. To display the conditions report re-run"
|
||||
+ " your application with 'debug' enabled.");
|
||||
|
||||
Reference in New Issue
Block a user