15
15
*/
16
16
package org .springframework .data .jpa .repository .query ;
17
17
18
- import static org .hamcrest .CoreMatchers .*;
19
- import static org .junit .Assert .*;
18
+ import static org .assertj .core .api .Assertions .*;
20
19
import static org .mockito .ArgumentMatchers .*;
21
20
import static org .mockito .Mockito .*;
22
21
23
22
import java .lang .reflect .Method ;
23
+ import java .util .List ;
24
24
25
25
import javax .persistence .EntityManager ;
26
26
import javax .persistence .EntityManagerFactory ;
27
27
import javax .persistence .metamodel .Metamodel ;
28
28
29
29
import org .junit .Before ;
30
- import org .junit .Rule ;
31
30
import org .junit .Test ;
32
- import org .junit .rules .ExpectedException ;
33
31
import org .junit .runner .RunWith ;
34
32
import org .mockito .Mock ;
35
33
import org .mockito .junit .MockitoJUnitRunner ;
36
- import org .springframework .data .domain .Page ;
37
- import org .springframework .data .domain .Pageable ;
34
+ import org .springframework .data .domain .Sort ;
38
35
import org .springframework .data .jpa .domain .sample .User ;
39
36
import org .springframework .data .jpa .provider .QueryExtractor ;
40
37
import org .springframework .data .jpa .repository .Query ;
53
50
*
54
51
* @author Oliver Gierke
55
52
* @author Thomas Darimont
53
+ * @author Jens Schauder
56
54
*/
57
55
@ RunWith (MockitoJUnitRunner .class )
58
56
public class JpaQueryLookupStrategyUnitTests {
@@ -65,8 +63,6 @@ public class JpaQueryLookupStrategyUnitTests {
65
63
@ Mock Metamodel metamodel ;
66
64
@ Mock ProjectionFactory projectionFactory ;
67
65
68
- public @ Rule ExpectedException exception = ExpectedException .none ();
69
-
70
66
@ Before
71
67
public void setUp () {
72
68
@@ -87,27 +83,23 @@ public void invalidAnnotatedQueryCausesException() throws Exception {
87
83
Throwable reference = new RuntimeException ();
88
84
when (em .createQuery (anyString ())).thenThrow (reference );
89
85
90
- try {
91
- strategy .resolveQuery (method , metadata , projectionFactory , namedQueries );
92
- } catch (Exception e ) {
93
- assertThat (e , is (instanceOf (IllegalArgumentException .class )));
94
- assertThat (e .getCause (), is (reference ));
95
- }
86
+ assertThatExceptionOfType (IllegalArgumentException .class )
87
+ .isThrownBy (() -> strategy .resolveQuery (method , metadata , projectionFactory , namedQueries ))
88
+ .withCause (reference );
96
89
}
97
90
98
91
@ Test // DATAJPA-554
99
92
public void sholdThrowMorePreciseExceptionIfTryingToUsePaginationInNativeQueries () throws Exception {
100
93
101
94
QueryLookupStrategy strategy = JpaQueryLookupStrategy .create (em , Key .CREATE_IF_NOT_FOUND , extractor ,
102
95
EVALUATION_CONTEXT_PROVIDER );
103
- Method method = UserRepository .class .getMethod ("findByInvalidNativeQuery" , String .class , Pageable .class );
96
+ Method method = UserRepository .class .getMethod ("findByInvalidNativeQuery" , String .class , Sort .class );
104
97
RepositoryMetadata metadata = new DefaultRepositoryMetadata (UserRepository .class );
105
98
106
- exception .expect (InvalidJpaQueryMethodException .class );
107
- exception .expectMessage ("Cannot use native queries with dynamic sorting and/or pagination in method" );
108
- exception .expectMessage (method .toString ());
109
-
110
- strategy .resolveQuery (method , metadata , projectionFactory , namedQueries );
99
+ assertThatExceptionOfType (InvalidJpaQueryMethodException .class )
100
+ .isThrownBy (() -> strategy .resolveQuery (method , metadata , projectionFactory , namedQueries ))
101
+ .withMessageContaining ("Cannot use native queries with dynamic sorting in method" )
102
+ .withMessageContaining (method .toString ());
111
103
}
112
104
113
105
interface UserRepository extends Repository <User , Long > {
@@ -116,6 +108,6 @@ interface UserRepository extends Repository<User, Long> {
116
108
User findByFoo (String foo );
117
109
118
110
@ Query (value = "select u.* from User u" , nativeQuery = true )
119
- Page <User > findByInvalidNativeQuery (String param , Pageable page );
111
+ List <User > findByInvalidNativeQuery (String param , Sort sort );
120
112
}
121
113
}
0 commit comments