0

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.

2
  • 3
    why there are two variables with same name predicate ? share actual code Commented May 23 at 3:36
  • typo error, I fix that already Commented May 23 at 12:16

1 Answer 1

0

You're encountering this error because the method CriteriaBuilder.isFalse() expects a parameter of type Expression<Boolean>, but you're passing a mock of type Path<Object>. Although Path<Boolean> is a subtype of Expression<Boolean>, your mock is declared as Path<Object>, and that's why the compiler complains.

Sign up to request clarification or add additional context in comments.

1 Comment

How could it be solved?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.