Skip to content

Commit

Permalink
Verify that mismatched return types are caught.
Browse files Browse the repository at this point in the history
Verify that when a return type doesn't match the query's type, an exception is thrown.

See #1184
  • Loading branch information
gregturn committed Jul 26, 2023
1 parent a217fc4 commit a096ce2
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.data.jpa.domain.sample.EmployeeWithName;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand Down Expand Up @@ -263,12 +264,30 @@ void derivedQueryLikeWithEmptyStringMatch() {
"Bilbo Baggins");
}

@Test // GH-1184
void mismatchedReturnTypeShouldCauseException() {
assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> repository.customQueryWithMismatchedReturnType());
}

@Test // GH-1184
void alignedReturnTypeShouldWork() {
assertThat(repository.customQueryWithAlignedReturnType()).containsExactly(new Object[][] {
{ "Frodo Baggins", "Frodo Baggins with suffix" }, { "Bilbo Baggins", "Bilbo Baggins with suffix" } });
}

@Transactional
public interface EmployeeWithNullLikeRepository extends JpaRepository<EmployeeWithName, Integer> {

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

@Query("select e.name, concat(e.name, ' with suffix') from EmployeeWithName e")
List<EmployeeWithName> customQueryWithMismatchedReturnType();

@Query("select e.name, concat(e.name, ' with suffix') from EmployeeWithName e")
List<Object[]> customQueryWithAlignedReturnType();

@Query(value = "select * from EmployeeWithName as e where e.name like %:partialName%", nativeQuery = true)
List<EmployeeWithName> customQueryWithNullableParamInNative(@Nullable @Param("partialName") String partialName);

Expand Down

0 comments on commit a096ce2

Please sign in to comment.