Spring Data JPA Criteria API mockito order by id column failing with `Type must not be null! ` error

20 Views Asked by At

Spring Data Jpa with criteria api Junits failed with Type must not be null! error, failure is mainly because of default sort order column i.e id(Long). Here I am using **database view on entity class**

ERROR:

Type must not be null!
java.lang.IllegalArgumentException: Type must not be null!
    at org.springframework.util.Assert.notNull(Assert.java:201)
    at org.springframework.data.util.ClassTypeInformation.from(ClassTypeInformation.java:69)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331)
    at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:600)
    at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:553)

Mocks used for test

        when(em.getCriteriaBuilder()).thenReturn(criteriaBuilder);
        when(criteriaBuilder.createQuery(Long.class)).thenReturn(this.countQuery);
        when(criteriaBuilder.createQuery(ExampleView.class)).thenReturn(criteriaQuery);
        when(criteriaQuery.from(ExampleView.class)).thenReturn(root);
        when(root.get(any(String.class))).thenReturn(path);
        when(criteriaBuilder.upper(path)).thenReturn(path);
        when(path.getJavaType()).thenReturn(Long.class);
        when(em.createQuery(criteriaQuery)).thenReturn(query);
        TypedQuery<Long> typedCountQuery = (TypedQuery<Long>) Mockito.mock(TypedQuery.class);
        when(em.createQuery(criteriaQuery)).thenReturn(query);
        when(em.createQuery(countQuery)).thenReturn(typedCountQuery);
        when(typedCountQuery.getSingleResult()).thenReturn(1L);

Can someone help me on this.

0

There are 0 best solutions below