Juliette
Juliette

Reputation: 1026

Mockito Error "Value must not be null" on when thenReturn with any()

I have the following code in my unit tests to simulate database calls:

Mockito.when(valueRepository.findAllByDateBetweenAndValueContent_BoolVal(
        any(LocalDate.class),
        any(LocalDate.class),
        anyBoolean()
)).thenReturn(new ArrayList<>());

Whenever I try to run my code I get the following error:

org.springframework.dao.InvalidDataAccessApiUsageException: Value must not be null!; nested exception is java.lang.IllegalArgumentException: Value must not be null!

I tried messing around with the Return value and with the Inputs (any...) but I couldn't find a solution and I don't really understand what value is null / what spring is complaining about.

The valueRepository - variable was initialized correctly, I checked with debug that it is not null.

Upvotes: 0

Views: 3175

Answers (1)

Juliette
Juliette

Reputation: 1026

I found my error: I inserted the valueRepository variable with Autowired instead of MockBean:

wrong / before:

@Autowired
ValueRepository valueRepository;

correct / after:

@MockBean
ValueRepository valueRepository;

With this change it works now. The error message wasn't very helpful unfortunately

Upvotes: 1

Related Questions