Skip to content

Commit 9720c82

Browse files
committed
Fix Checkstyle for JPA test; typos
* Rework `try..catch` structures in the JPA test into an `assertThatIllegalArgumentException()` and `assertThatIllegalStateException()`
1 parent 8a4864e commit 9720c82

File tree

2 files changed

+35
-61
lines changed

2 files changed

+35
-61
lines changed

spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/HibernateJpaOperationsTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import org.junit.runner.RunWith;
2323

2424
import org.springframework.test.annotation.DirtiesContext;
25-
import org.springframework.test.context.ContextConfiguration;
26-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2725
import org.springframework.test.context.junit4.SpringRunner;
2826

2927
/**

spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package org.springframework.integration.jpa.core;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2021
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
21-
import static org.assertj.core.api.Assertions.fail;
2222
import static org.mockito.Mockito.mock;
2323

2424
import java.util.Collections;
@@ -63,7 +63,7 @@ public class JpaExecutorTests {
6363
private BeanFactory beanFactory;
6464

6565
/**
66-
* In this test, the {@link JpaExecutor}'s p\oll method will be called without
66+
* In this test, the {@link JpaExecutor}'s poll method will be called without
6767
* specifying a 'query', 'namedQuery' or 'entityClass' property. This should
6868
* result in an {@link IllegalArgumentException}.
6969
*/
@@ -72,70 +72,51 @@ public void testExecutePollWithNoEntityClassSpecified() {
7272
JpaExecutor jpaExecutor = new JpaExecutor(mock(EntityManager.class));
7373
jpaExecutor.setBeanFactory(mock(BeanFactory.class));
7474
jpaExecutor.afterPropertiesSet();
75-
try {
76-
jpaExecutor.poll();
77-
}
78-
catch (IllegalStateException e) {
79-
assertThat(e.getMessage()).as("Exception Message does not match.")
80-
.isEqualTo("For the polling operation, one of "
81-
+ "the following properties must be specified: "
82-
+ "query, namedQuery or entityClass.");
83-
return;
84-
}
85-
86-
fail("Was expecting an IllegalStateException to be thrown.");
75+
76+
assertThatIllegalStateException()
77+
.isThrownBy(jpaExecutor::poll)
78+
.withMessage("For the polling operation, one of "
79+
+ "the following properties must be specified: "
80+
+ "query, namedQuery or entityClass.");
8781
}
8882

8983
@Test
9084
public void testInstantiateJpaExecutorWithNullJpaOperations() {
91-
JpaOperations jpaOperations = null;
92-
93-
try {
94-
new JpaExecutor(jpaOperations);
95-
}
96-
catch (IllegalArgumentException e) {
97-
assertThat(e.getMessage()).isEqualTo("jpaOperations must not be null.");
98-
}
85+
assertThatIllegalArgumentException()
86+
.isThrownBy(() -> new JpaExecutor((JpaOperations) null))
87+
.withMessage("jpaOperations must not be null.");
9988
}
10089

10190
@Test
10291
public void testSetMultipleQueryTypes() {
103-
JpaExecutor executor = new JpaExecutor(mock(EntityManager.class));
92+
final JpaExecutor executor = new JpaExecutor(mock(EntityManager.class));
10493
executor.setJpaQuery("select s from Student s");
10594
assertThat(TestUtils.getPropertyValue(executor, "jpaQuery", String.class)).isNotNull();
10695

107-
try {
108-
executor.setNamedQuery("NamedQuery");
109-
}
110-
catch (IllegalArgumentException e) {
111-
assertThat(e.getMessage()).isEqualTo("You can define only one of the "
112-
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'");
113-
}
96+
assertThatIllegalArgumentException()
97+
.isThrownBy(() -> executor.setNamedQuery("NamedQuery"))
98+
.withMessage("You can define only one of the "
99+
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'");
114100

115101
assertThat(TestUtils.getPropertyValue(executor, "namedQuery")).isNull();
116102

117-
try {
118-
executor.setNativeQuery("select * from Student");
119-
}
120-
catch (IllegalArgumentException e) {
121-
assertThat(e.getMessage()).isEqualTo("You can define only one of the "
122-
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'");
123-
}
103+
assertThatIllegalArgumentException()
104+
.isThrownBy(() -> executor.setNativeQuery("select * from Student"))
105+
.withMessage("You can define only one of the "
106+
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'");
107+
124108
assertThat(TestUtils.getPropertyValue(executor, "nativeQuery")).isNull();
125109

126-
executor = new JpaExecutor(mock(EntityManager.class));
127-
executor.setNamedQuery("NamedQuery");
128-
assertThat(TestUtils.getPropertyValue(executor, "namedQuery", String.class)).isNotNull();
110+
final JpaExecutor executor2 = new JpaExecutor(mock(EntityManager.class));
111+
executor2.setNamedQuery("NamedQuery");
112+
assertThat(TestUtils.getPropertyValue(executor2, "namedQuery", String.class)).isNotNull();
129113

130-
try {
131-
executor.setJpaQuery("select s from Student s");
132-
}
133-
catch (IllegalArgumentException e) {
134-
assertThat(e.getMessage()).isEqualTo("You can define only one of the "
135-
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'");
136-
}
137-
assertThat(TestUtils.getPropertyValue(executor, "jpaQuery")).isNull();
114+
assertThatIllegalArgumentException()
115+
.isThrownBy(() -> executor2.setJpaQuery("select s from Student s"))
116+
.withMessage("You can define only one of the "
117+
+ "properties 'jpaQuery', 'nativeQuery', 'namedQuery'");
138118

119+
assertThat(TestUtils.getPropertyValue(executor2, "jpaQuery")).isNull();
139120
}
140121

141122
@Test
@@ -222,7 +203,7 @@ public void testResultStartingFromThirdRecordForJPAQuery() {
222203

223204
List<?> results = (List<?>) jpaExecutor.poll(MessageBuilder.withPayload("").build());
224205
assertThat(results).isNotNull();
225-
assertThat(results.size()).isEqualTo(1);
206+
assertThat(results).hasSize(1);
226207
}
227208

228209
@Test
@@ -235,7 +216,7 @@ public void testResultStartingFromThirdRecordForNativeQuery() {
235216

236217
List<?> results = (List<?>) jpaExecutor.poll(MessageBuilder.withPayload("").build());
237218
assertThat(results).isNotNull();
238-
assertThat(results.size()).isEqualTo(1);
219+
assertThat(results).hasSize(1);
239220
}
240221

241222
@Test
@@ -248,7 +229,7 @@ public void testResultStartingFromThirdRecordForNamedQuery() {
248229

249230
List<?> results = (List<?>) jpaExecutor.poll(MessageBuilder.withPayload("").build());
250231
assertThat(results).isNotNull();
251-
assertThat(results.size()).isEqualTo(1);
232+
assertThat(results).hasSize(1);
252233
}
253234

254235
@Test
@@ -267,14 +248,9 @@ public void testResultStartingFromThirdRecordUsingEntity() {
267248
@Test
268249
public void withNullMaxResultsExpression() {
269250
final JpaExecutor jpaExecutor = new JpaExecutor(mock(EntityManager.class));
270-
try {
271-
jpaExecutor.setMaxResultsExpression(null);
272-
}
273-
catch (Exception e) {
274-
assertThat(e.getMessage()).isEqualTo("maxResultsExpression cannot be null");
275-
return;
276-
}
277-
fail("Expected the test case to throw an exception");
251+
assertThatIllegalArgumentException()
252+
.isThrownBy(() -> jpaExecutor.setMaxResultsExpression(null))
253+
.withMessage("maxResultsExpression cannot be null");
278254
}
279255

280256
@Test

0 commit comments

Comments
 (0)