Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Mockito any with not null constraint

I am writing test cases from last few days. I want to know is there any equivalent of org.mockito.Matchers.any with not null constraint?

I mean if you say:

when(reviewBuilder.saveReviewModel(any())).thenReturn(2L);

Then saveReviewModel can take any objects including null. Is there a way to pass only not null objects to a method using Mockito 1.10.19 library?

like image 278
user3681970 Avatar asked Jan 15 '17 12:01

user3681970


People also ask

Does Mockito any () match null?

Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments.

What does Mockito any () return?

any always returns null .

Is Mockito better than EasyMock?

method. Mockito is the most popular mocking framework used for testing Java applications. It is better than any other testing and mocking framework, such as EasyMock.

What is argThat in Mockito?

The argThat argument matcher in Mockito lets you create advanced argument matchers that run a function on passed arguments, and checks if the function returns true . If you have a complicated class that can't be easily checked using . equals() , a custom matcher can be a useful tool.


1 Answers

In Mockito 1.9.5 there are isNotNull() and isNotNull(java.lang.Class clazz) which do the null check.

From version 2.1.0 anyX() and any(SomeType.class) matchers reject nulls and check type. See Incompatible changes with 1.10 paragraph on What's new in Mockito 2 page.

like image 114
Oleksandr Tarasenko Avatar answered Sep 21 '22 15:09

Oleksandr Tarasenko