A Mocked repository returns null - Springboot JUnit Mockito

Im trying to use the save from my repository EmpresaRepository, but it returns null, heres my repository heres my repository and this is my test

And this is the output

Upvotes: 1

Views: 2157

Answers (1)

Jan Schmitz
Jan Schmitz

Reputation: 1641

This is the expected behaviour.

By using @MockBean you create and add a Mock of your EmpresaRepository into the ApplicationContext. This means you execute the ::save() Method of your mock which returns null bey default.

Rule of thumb: Never Mock the thing you want to test.

The test should work if you replace the @MockBean annotation with Autowired.


Btw. please don't post screenshots of your code. It's annoying to switch between different tabs and also I'm not able to copy parts of your code.

Upvotes: 1

Related Questions