17
17
package org .springframework .integration .jpa .core ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
20
21
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
21
- import static org .assertj .core .api .Assertions .fail ;
22
22
import static org .mockito .Mockito .mock ;
23
23
24
24
import java .util .Collections ;
@@ -63,7 +63,7 @@ public class JpaExecutorTests {
63
63
private BeanFactory beanFactory ;
64
64
65
65
/**
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
67
67
* specifying a 'query', 'namedQuery' or 'entityClass' property. This should
68
68
* result in an {@link IllegalArgumentException}.
69
69
*/
@@ -72,70 +72,51 @@ public void testExecutePollWithNoEntityClassSpecified() {
72
72
JpaExecutor jpaExecutor = new JpaExecutor (mock (EntityManager .class ));
73
73
jpaExecutor .setBeanFactory (mock (BeanFactory .class ));
74
74
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." );
87
81
}
88
82
89
83
@ Test
90
84
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." );
99
88
}
100
89
101
90
@ Test
102
91
public void testSetMultipleQueryTypes () {
103
- JpaExecutor executor = new JpaExecutor (mock (EntityManager .class ));
92
+ final JpaExecutor executor = new JpaExecutor (mock (EntityManager .class ));
104
93
executor .setJpaQuery ("select s from Student s" );
105
94
assertThat (TestUtils .getPropertyValue (executor , "jpaQuery" , String .class )).isNotNull ();
106
95
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'" );
114
100
115
101
assertThat (TestUtils .getPropertyValue (executor , "namedQuery" )).isNull ();
116
102
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
+
124
108
assertThat (TestUtils .getPropertyValue (executor , "nativeQuery" )).isNull ();
125
109
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 ();
129
113
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'" );
138
118
119
+ assertThat (TestUtils .getPropertyValue (executor2 , "jpaQuery" )).isNull ();
139
120
}
140
121
141
122
@ Test
@@ -222,7 +203,7 @@ public void testResultStartingFromThirdRecordForJPAQuery() {
222
203
223
204
List <?> results = (List <?>) jpaExecutor .poll (MessageBuilder .withPayload ("" ).build ());
224
205
assertThat (results ).isNotNull ();
225
- assertThat (results . size ()). isEqualTo (1 );
206
+ assertThat (results ). hasSize (1 );
226
207
}
227
208
228
209
@ Test
@@ -235,7 +216,7 @@ public void testResultStartingFromThirdRecordForNativeQuery() {
235
216
236
217
List <?> results = (List <?>) jpaExecutor .poll (MessageBuilder .withPayload ("" ).build ());
237
218
assertThat (results ).isNotNull ();
238
- assertThat (results . size ()). isEqualTo (1 );
219
+ assertThat (results ). hasSize (1 );
239
220
}
240
221
241
222
@ Test
@@ -248,7 +229,7 @@ public void testResultStartingFromThirdRecordForNamedQuery() {
248
229
249
230
List <?> results = (List <?>) jpaExecutor .poll (MessageBuilder .withPayload ("" ).build ());
250
231
assertThat (results ).isNotNull ();
251
- assertThat (results . size ()). isEqualTo (1 );
232
+ assertThat (results ). hasSize (1 );
252
233
}
253
234
254
235
@ Test
@@ -267,14 +248,9 @@ public void testResultStartingFromThirdRecordUsingEntity() {
267
248
@ Test
268
249
public void withNullMaxResultsExpression () {
269
250
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" );
278
254
}
279
255
280
256
@ Test
0 commit comments