Search code examples
javajava-8mockitoreactive-programmingspring-webflux

Testing custom predicate using mocked Exception failing due to incorrect Mock class


I have created a custom Predicate below and want to test it using mockito. I am creating the mocks of the specific exception classes since these dont have public constructor. After running the test assert is failing since the predicate is returning false instead of true. On printing the class of the mocked exception it has WebClientResponseException$ServiceUnavailable$MockitoMock$54675.Seems like the mock is not recognized correctly. Am I doing something wrong here?

PredicateTest

@ExtendsWith(MockitoExtention.class)
class PredicateTest{

@InjectMocks
CustomPredicate customPredicate;



@Test
public void testPredicate(){

final ServiceUnavailable serviceUnavailable = mock(ServiceUnAvailable.class);

assertTrue(customPredicate.test(serviceUnavailable))
    
}  
}

CustomPredicate

CustomPredicate implements Predicate<Throwable>{

private static final List<Class<?>> Exceptions= Arrays.asList(WebClientResponseException.ServiceUnavailable.class);

private static final Predicate<? super Throwable> ClassToControl= throwable -> Exception.contain(throwable.getClass());


@Override
public boolean test(Throwable t){

return ExceptionUtils.getThrowableList(t).stream().anyMatch(ClassToControl);

}


}

Solution

  • So the issue was I was not having mockito-inline jar in the pom. Not sure why but this solved the issue