0

I'm new to programming, I want to make tests for my Specification class, but I can't figure out where I should start...how to approach this test?

 public class MySpecification implements Specification<R> {
    
        MyRequest request;
    
        public MySpecification (MyRequest request) {
            this.request = request;
        }
    
        @Override
        public Predicate toPredicate(Root<R> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
            List<Predicate> predicates = new ArrayList<>();
    
            if (StringUtils.isNotEmpty(request.getEvent())) {
                predicates.add(criteriaBuilder.equal(root.get("event"), request.getEvent()));
            }
        return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
    }
}

I created mock objects that I will most likely have to work with

    @Mock
    Specification<R> spec;

    @Mock
    Root<R> root;

    @Mock
    CriteriaQuery<?> query;

    @Mock
    CriteriaBuilder builder;

    @Mock
    Predicate predicate;

    @Mock
    MyRequest request;

I would like to see one or two examples of test implementation for this case, thank you!

1 Answers1

0
@InjectMocks
MySpecification  mySpecification;

@Test
void test(){

Predicate  predicate = mySpecification.toPredicate(pass arguments);
assertNotNull(predicate);
}
Sandeep
  • 1
  • 1
  • thank you for answer! But i catch java.lang.NullPointerException Predicate predicate = mySpecification.toPredicate(root, query, criteriaBuilder); – Marzepanio Tortus Aug 16 '22 at 07:57