Skip to content

Commit a096ce2

Browse files
committed
Verify that mismatched return types are caught.
Verify that when a return type doesn't match the query's type, an exception is thrown. See #1184
1 parent a217fc4 commit a096ce2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/QueryWithNullLikeIntegrationTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.context.annotation.Bean;
3232
import org.springframework.context.annotation.ComponentScan.Filter;
3333
import org.springframework.context.annotation.FilterType;
34+
import org.springframework.core.convert.ConversionFailedException;
3435
import org.springframework.data.jpa.domain.sample.EmployeeWithName;
3536
import org.springframework.data.jpa.repository.JpaRepository;
3637
import org.springframework.data.jpa.repository.Query;
@@ -263,12 +264,30 @@ void derivedQueryLikeWithEmptyStringMatch() {
263264
"Bilbo Baggins");
264265
}
265266

267+
@Test // GH-1184
268+
void mismatchedReturnTypeShouldCauseException() {
269+
assertThatExceptionOfType(ConversionFailedException.class)
270+
.isThrownBy(() -> repository.customQueryWithMismatchedReturnType());
271+
}
272+
273+
@Test // GH-1184
274+
void alignedReturnTypeShouldWork() {
275+
assertThat(repository.customQueryWithAlignedReturnType()).containsExactly(new Object[][] {
276+
{ "Frodo Baggins", "Frodo Baggins with suffix" }, { "Bilbo Baggins", "Bilbo Baggins with suffix" } });
277+
}
278+
266279
@Transactional
267280
public interface EmployeeWithNullLikeRepository extends JpaRepository<EmployeeWithName, Integer> {
268281

269282
@Query("select e from EmployeeWithName e where e.name like %:partialName%")
270283
List<EmployeeWithName> customQueryWithNullableParam(@Nullable @Param("partialName") String partialName);
271284

285+
@Query("select e.name, concat(e.name, ' with suffix') from EmployeeWithName e")
286+
List<EmployeeWithName> customQueryWithMismatchedReturnType();
287+
288+
@Query("select e.name, concat(e.name, ' with suffix') from EmployeeWithName e")
289+
List<Object[]> customQueryWithAlignedReturnType();
290+
272291
@Query(value = "select * from EmployeeWithName as e where e.name like %:partialName%", nativeQuery = true)
273292
List<EmployeeWithName> customQueryWithNullableParamInNative(@Nullable @Param("partialName") String partialName);
274293

0 commit comments

Comments
 (0)