some problem with mock test for specification class

347 Views Asked by At

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

There are 1 best solutions below

1
Sandeep On
@InjectMocks
MySpecification  mySpecification;

@Test
void test(){

Predicate  predicate = mySpecification.toPredicate(pass arguments);
assertNotNull(predicate);
}