Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.metamodel.SingularAttribute;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.stream.Stream;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -80,6 +82,7 @@ class SimpleJpaRepositoryUnitTests {
@Mock CrudMethodMetadata metadata;
@Mock EntityGraph<User> entityGraph;
@Mock org.springframework.data.jpa.repository.EntityGraph entityGraphAnnotation;
@Mock SingularAttribute idAttribute;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -262,4 +265,38 @@ static Stream<Arguments> modifyingMethods() {
private static String formatName(Method method) {
return method.toString().replaceAll("public ", "").replaceAll(SimpleJpaRepository.class.getName() + ".", "");
}

@Test // GH-4134
@SuppressWarnings({"unchecked"})
void existsByIdReturnsTrueWhenEntityExists() {

when(information.getIdAttribute()).thenReturn(idAttribute);
when(information.getEntityName()).thenReturn("User");
when(information.getIdAttributeNames()).thenReturn(List.of("id"));
when(information.hasCompositeId()).thenReturn(false);
when(em.createQuery(anyString(), eq(Long.class))).thenReturn(countQuery);
when(countQuery.getSingleResult()).thenReturn(1L);

boolean result = repo.existsById(1);

verify(countQuery).getSingleResult();
assertThat(result).isTrue();
}

@Test // GH-4134
@SuppressWarnings({"unchecked"})
void existsByIdReturnsFalseWhenEntityDoesNotExist() {

when(information.getIdAttribute()).thenReturn(idAttribute);
when(information.getEntityName()).thenReturn("User");
when(information.getIdAttributeNames()).thenReturn(List.of("id"));
when(information.hasCompositeId()).thenReturn(false);
when(em.createQuery(anyString(), eq(Long.class))).thenReturn(countQuery);
when(countQuery.getSingleResult()).thenReturn(0L);

boolean result = repo.existsById(1);

verify(countQuery).getSingleResult();
assertThat(result).isFalse();
}
}
Loading