I have a method that creates a Specification (Spring JPA) from my custom object to check if its attribute is False with the method. Using Mockito, I tried do the unit test below.
void unitTestIsNotDeleted() {
Root root = mock(Root.class)
CriteriaBuilder cb = mock(CriteriaBuilder.class)
Predicate predicate = mock(Predicate.class);
Path<Object> path = mock(Path.class);
when(root.get("deleted")).thenReturn(path);
when(cb.isFalse(path)).thenReturn(predicate);
Specification<MyObj> specification = CustomSpecifications.isNotDeleted();
Assertions.assertNotNull(specification);
Predicate result = specification.toPredicate(root, query, cb);
Assertions.assertNotNull(result);
Assertions.assertEquals(predicate, result);
}
I got the exception below when I try run the code. However I did similar tests with CriteriaBuilder.equals(), but I haven't gotten this kind of exception. Any solutions to this trouble?
java: incompatible types: jakarta.persistence.criteria.Path<java.lang.Object> cannot be converted to jakarta.persistence.criteria.Expression<java.lang.Boolean>
I tried do something with a intermediary Path<Boolean> converting the Path<Object> into it, but I got PotentialStubbingProblem because misuse of Mocks. I also tried Mockito.lenient(), which runs the test, yet result in a failed test.
predicate? share actual code