Skip to content

Commit b162e2b

Browse files
Refactor tests to use assertThatException instead of fail()
* Use `assertThatException` * Replace `isThrownBy` method * Change to text block and remove `this.` prefix * Fix whitespace Checkstyle Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 0d2b21a commit b162e2b

File tree

13 files changed

+182
-347
lines changed

13 files changed

+182
-347
lines changed

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
import static org.assertj.core.api.Assertions.assertThat;
3939
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
40-
import static org.assertj.core.api.Assertions.fail;
4140
import static org.mockito.Mockito.mock;
4241

4342
/**
@@ -137,7 +136,7 @@ public void testSetReturningResultSetRowMappersWithEmptyMap() {
137136
DataSource datasource = mock(DataSource.class);
138137
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource);
139138

140-
Map<String, RowMapper<?>> rowmappers = new HashMap<String, RowMapper<?>>();
139+
Map<String, RowMapper<?>> rowmappers = new HashMap<>();
141140

142141
storedProcExecutor.setReturningResultSetRowMappers(rowmappers);
143142

@@ -149,17 +148,9 @@ public void testSetReturningResultSetRowMappersWithEmptyMap() {
149148
public void testSetSqlParameterSourceFactoryWithNullParameter() {
150149
DataSource datasource = mock(DataSource.class);
151150
StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource);
152-
153-
try {
154-
storedProcExecutor.setSqlParameterSourceFactory(null);
155-
}
156-
catch (IllegalArgumentException e) {
157-
assertThat(e.getMessage()).isEqualTo("sqlParameterSourceFactory must not be null.");
158-
return;
159-
}
160-
161-
fail("Exception expected.");
162-
151+
assertThatIllegalArgumentException()
152+
.isThrownBy(() -> storedProcExecutor.setSqlParameterSourceFactory(null))
153+
.withMessage("sqlParameterSourceFactory must not be null.");
163154
}
164155

165156
@Test

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/InnerPollerParserTests.java

Lines changed: 87 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import org.springframework.core.io.ByteArrayResource;
2525
import org.springframework.core.io.Resource;
2626

27-
import static org.assertj.core.api.Assertions.assertThat;
28-
import static org.assertj.core.api.Assertions.fail;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2928

3029
/**
3130
* @author Gary Russell
@@ -42,120 +41,100 @@ public void testRefGood() {
4241

4342
@Test
4443
public void testRefExtraAttribute() {
45-
try {
46-
// Load context from a String to avoid IDEs reporting the invalid configuration
47-
String badContext =
48-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
49-
"<beans xmlns=\"http://www.springframework.org/schema/beans\"" +
50-
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
51-
" xmlns:int=\"http://www.springframework.org/schema/integration\"" +
52-
" xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" +
53-
" xsi:schemaLocation=\"http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd" +
54-
" http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd" +
55-
" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd\">" +
56-
"" +
57-
" <int:poller id=\"outer\" fixed-rate=\"5000\"/>" +
58-
"" +
59-
" <int:channel id=\"someChannel\"/>" +
60-
"" +
61-
" <int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" +
62-
" query=\"select 1\">" +
63-
" <int:poller ref=\"outer\" fixed-rate=\"1000\"/>" + // <<<<< fixed-rate not allowed here
64-
" </int-jdbc:inbound-channel-adapter>" +
65-
"" +
66-
" <bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" +
67-
" <constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" +
68-
" </bean>" +
69-
"</beans>";
70-
71-
Resource resource = new ByteArrayResource(badContext.getBytes());
72-
new GenericXmlApplicationContext(resource).close();
73-
fail("Expected Failure to load ApplicationContext");
74-
}
75-
catch (BeanDefinitionParsingException bdpe) {
76-
assertThat(bdpe.getMessage()
77-
.startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other " +
78-
"attributes."))
79-
.isTrue();
80-
}
44+
// Load context from a String to avoid IDEs reporting the invalid configuration
45+
String badContext = """
46+
<?xml version="1.0" encoding="UTF-8"?>
47+
<beans xmlns="http://www.springframework.org/schema/beans"
48+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
49+
xmlns:int="http://www.springframework.org/schema/integration"
50+
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
51+
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
52+
http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
53+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
54+
55+
<int:poller id="outer" fixed-rate="5000"/>
56+
57+
<int:channel id="someChannel"/>
58+
59+
<int-jdbc:inbound-channel-adapter channel="someChannel" jdbc-operations="ops" query="select 1">
60+
<int:poller ref="outer" fixed-rate="1000"/>
61+
</int-jdbc:inbound-channel-adapter>
62+
63+
<bean id="ops" class="org.mockito.Mockito" factory-method="mock">
64+
<constructor-arg value="org.springframework.jdbc.core.JdbcOperations"/>
65+
</bean>
66+
</beans>
67+
""";
68+
Resource resource = new ByteArrayResource(badContext.getBytes());
69+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
70+
.isThrownBy(() -> new GenericXmlApplicationContext(resource))
71+
.withMessageStartingWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes.");
8172
}
8273

8374
@Test
8475
public void testRefDefaultTrue() {
85-
try {
86-
// Load context from a String to avoid IDEs reporting the invalid configuration
87-
String badContext =
88-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
89-
"<beans xmlns=\"http://www.springframework.org/schema/beans\"" +
90-
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
91-
" xmlns:int=\"http://www.springframework.org/schema/integration\"" +
92-
" xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" +
93-
" xsi:schemaLocation=\"http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd" +
94-
" http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd" +
95-
" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd\">" +
96-
"" +
97-
" <int:poller id=\"outer\" fixed-rate=\"5000\"/>" +
98-
"" +
99-
" <int:channel id=\"someChannel\"/>" +
100-
"" +
101-
" <int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" +
102-
" query=\"select 1\">" +
103-
" <int:poller ref=\"outer\" default=\"true\"/>" + // <<<<< default true not allowed here
104-
" </int-jdbc:inbound-channel-adapter>" +
105-
"" +
106-
" <bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" +
107-
" <constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" +
108-
" </bean>" +
109-
"</beans>";
110-
111-
Resource resource = new ByteArrayResource(badContext.getBytes());
112-
new GenericXmlApplicationContext(resource).close();
113-
fail("Expected Failure to load ApplicationContext");
114-
}
115-
catch (BeanDefinitionParsingException bdpe) {
116-
assertThat(bdpe.getMessage()
117-
.startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes."))
118-
.isTrue();
119-
}
76+
// Load context from a String to avoid IDEs reporting the invalid configuration
77+
String badContext = """
78+
<?xml version="1.0" encoding="UTF-8"?>
79+
<beans xmlns="http://www.springframework.org/schema/beans"
80+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
81+
xmlns:int="http://www.springframework.org/schema/integration"
82+
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
83+
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
84+
http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
85+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
86+
87+
<int:poller id="outer" fixed-rate="5000"/>
88+
89+
<int:channel id="someChannel"/>
90+
91+
<int-jdbc:inbound-channel-adapter channel="someChannel" jdbc-operations="ops" query="select 1">
92+
<int:poller ref="outer" default="true"/>
93+
</int-jdbc:inbound-channel-adapter>
94+
95+
<bean id="ops" class="org.mockito.Mockito" factory-method="mock">
96+
<constructor-arg value="org.springframework.jdbc.core.JdbcOperations"/>
97+
</bean>
98+
</beans>
99+
""";
100+
101+
Resource resource = new ByteArrayResource(badContext.getBytes());
102+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
103+
.isThrownBy(() -> new GenericXmlApplicationContext(resource))
104+
.withMessageStartingWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes.");
120105
}
121106

122107
@Test
123108
public void testRefExtraAttributeAndDefaultFalse() {
124-
try {
125-
// Load context from a String to avoid IDEs reporting the invalid configuration
126-
String badContext =
127-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
128-
"<beans xmlns=\"http://www.springframework.org/schema/beans\"" +
129-
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
130-
" xmlns:int=\"http://www.springframework.org/schema/integration\"" +
131-
" xmlns:int-jdbc=\"http://www.springframework.org/schema/integration/jdbc\"" +
132-
" xsi:schemaLocation=\"http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd" +
133-
" http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd" +
134-
" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd\">" +
135-
"" +
136-
" <int:poller id=\"outer\" fixed-rate=\"5000\"/>" +
137-
"" +
138-
" <int:channel id=\"someChannel\"/>" +
139-
"" +
140-
" <int-jdbc:inbound-channel-adapter channel=\"someChannel\" jdbc-operations=\"ops\"" +
141-
" query=\"select 1\">" +
142-
" <int:poller ref=\"outer\" default=\"false\" fixed-rate=\"1000\"/>" + // <<<<< fixed-rate not allowed here
143-
" </int-jdbc:inbound-channel-adapter>" +
144-
"" +
145-
" <bean id=\"ops\" class=\"org.mockito.Mockito\" factory-method=\"mock\">" +
146-
" <constructor-arg value=\"org.springframework.jdbc.core.JdbcOperations\"/>" +
147-
" </bean>" +
148-
"</beans>";
149-
150-
Resource resource = new ByteArrayResource(badContext.getBytes());
151-
new GenericXmlApplicationContext(resource).close();
152-
fail("Expected Failure to load ApplicationContext");
153-
}
154-
catch (BeanDefinitionParsingException bdpe) {
155-
assertThat(bdpe.getMessage()
156-
.startsWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes."))
157-
.isTrue();
158-
}
109+
// Load context from a String to avoid IDEs reporting the invalid configuration
110+
String badContext = """
111+
<?xml version="1.0" encoding="UTF-8"?>
112+
<beans xmlns="http://www.springframework.org/schema/beans"
113+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
114+
xmlns:int="http://www.springframework.org/schema/integration"
115+
xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
116+
xsi:schemaLocation="http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
117+
http://www.springframework.org/schema/integration/jdbc https://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
118+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
119+
120+
<int:poller id="outer" fixed-rate="5000"/>
121+
122+
<int:channel id="someChannel"/>
123+
124+
<int-jdbc:inbound-channel-adapter channel="someChannel" jdbc-operations="ops" query="select 1">
125+
<int:poller ref="outer" default="false" fixed-rate="1000"/>
126+
</int-jdbc:inbound-channel-adapter>
127+
128+
<bean id="ops" class="org.mockito.Mockito" factory-method="mock">
129+
<constructor-arg value="org.springframework.jdbc.core.JdbcOperations"/>
130+
</bean>
131+
</beans>
132+
""";
133+
134+
Resource resource = new ByteArrayResource(badContext.getBytes());
135+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
136+
.isThrownBy(() -> new GenericXmlApplicationContext(resource))
137+
.withMessageStartingWith("Configuration problem: A 'poller' element that provides a 'ref' must have no other attributes.");
159138
}
160139

161140
}

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/JdbcTypesEnumTests.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
24-
import static org.assertj.core.api.Assertions.fail;
24+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2525

2626
public class JdbcTypesEnumTests {
2727

@@ -45,31 +45,17 @@ public void testConvertToJdbcTypesEnumWithInvalidParameter() {
4545
@Test
4646
public void testConvertToJdbcTypesEnumWithNullParameter() {
4747

48-
try {
49-
JdbcTypesEnum.convertToJdbcTypesEnum(null);
50-
}
51-
catch (IllegalArgumentException e) {
52-
assertThat(e.getMessage()).isEqualTo("Parameter sqlTypeAsString, must not be null nor empty");
53-
return;
54-
}
55-
56-
fail("Expected Exception");
57-
48+
assertThatIllegalArgumentException()
49+
.isThrownBy(() -> JdbcTypesEnum.convertToJdbcTypesEnum(null))
50+
.withMessage("Parameter sqlTypeAsString, must not be null nor empty");
5851
}
5952

6053
@Test
6154
public void testConvertToJdbcTypesEnumWithEmptyParameter() {
6255

63-
try {
64-
JdbcTypesEnum.convertToJdbcTypesEnum(" ");
65-
}
66-
catch (IllegalArgumentException e) {
67-
assertThat(e.getMessage()).isEqualTo("Parameter sqlTypeAsString, must not be null nor empty");
68-
return;
69-
}
70-
71-
fail("Expected Exception");
72-
56+
assertThatIllegalArgumentException()
57+
.isThrownBy(() -> JdbcTypesEnum.convertToJdbcTypesEnum(" "))
58+
.withMessage("Parameter sqlTypeAsString, must not be null nor empty");
7359
}
7460

7561
}

spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/config/StoredProcInvalidConfigsTests.java

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
import org.springframework.core.io.ClassPathResource;
3030
import org.springframework.core.io.InputStreamResource;
3131

32-
import static org.assertj.core.api.Assertions.assertThat;
33-
import static org.assertj.core.api.Assertions.fail;
32+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3433

3534
/**
3635
* @author Artem Bilan
@@ -39,54 +38,32 @@
3938
public class StoredProcInvalidConfigsTests {
4039

4140
@Test
42-
public void testProcedureNameAndExpressionExclusivity() throws Exception {
43-
try {
44-
this.bootStrap("nameAndExpressionExclusivity");
45-
fail("Expected a BeanDefinitionParsingException to be thrown.");
46-
}
47-
catch (BeanDefinitionParsingException e) {
48-
assertThat(e.getMessage()
49-
.contains("Exactly one of 'stored-procedure-name' or 'stored-procedure-name-expression' is " +
50-
"required"))
51-
.isTrue();
52-
}
41+
public void testProcedureNameAndExpressionExclusivity() {
42+
43+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
44+
.isThrownBy(() -> bootStrap("nameAndExpressionExclusivity"))
45+
.withMessageContaining("Exactly one of 'stored-procedure-name' or 'stored-procedure-name-expression' is required");
5346
}
5447

5548
@Test
56-
public void testReturnTypeForInParameter() throws Exception {
57-
try {
58-
this.bootStrap("returnTypeForInParameter");
59-
fail("Expected a BeanDefinitionParsingException to be thrown.");
60-
}
61-
catch (BeanDefinitionParsingException e) {
62-
assertThat(e.getMessage()
63-
.contains("'return-type' attribute can't be provided for IN 'sql-parameter-definition' element."))
64-
.isTrue();
65-
}
49+
public void testReturnTypeForInParameter() {
50+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
51+
.isThrownBy(() -> bootStrap("returnTypeForInParameter"))
52+
.withMessageContaining("'return-type' attribute can't be provided for IN 'sql-parameter-definition' element.");
6653
}
6754

6855
@Test
69-
public void testTypeNameAndScaleExclusivity() throws Exception {
70-
try {
71-
this.bootStrap("typeNameAndScaleExclusivity");
72-
fail("Expected a BeanDefinitionParsingException to be thrown.");
73-
}
74-
catch (BeanDefinitionParsingException e) {
75-
assertThat(e.getMessage().contains("'type-name' and 'scale' attributes are mutually exclusive " +
76-
"for 'sql-parameter-definition' element.")).isTrue();
77-
}
56+
public void testTypeNameAndScaleExclusivity() {
57+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
58+
.isThrownBy(() -> bootStrap("typeNameAndScaleExclusivity"))
59+
.withMessageContaining("'type-name' and 'scale' attributes are mutually exclusive for 'sql-parameter-definition' element.");
7860
}
7961

8062
@Test
81-
public void testReturnTypeAndScaleExclusivity() throws Exception {
82-
try {
83-
this.bootStrap("returnTypeAndScaleExclusivity");
84-
fail("Expected a BeanDefinitionParsingException to be thrown.");
85-
}
86-
catch (BeanDefinitionParsingException e) {
87-
assertThat(e.getMessage().contains("'returnType' and 'scale' attributes are mutually exclusive " +
88-
"for 'sql-parameter-definition' element.")).isTrue();
89-
}
63+
public void testReturnTypeAndScaleExclusivity() {
64+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
65+
.isThrownBy(() -> bootStrap("returnTypeAndScaleExclusivity"))
66+
.withMessageContaining("'returnType' and 'scale' attributes are mutually exclusive for 'sql-parameter-definition' element.");
9067
}
9168

9269
private ApplicationContext bootStrap(String configProperty) throws Exception {

0 commit comments

Comments
 (0)