How to use @InjectMocks with Mockito - Tutorial

preview_player
Показать описание
Learn about how you can use @InjectMocks to automatically add services to classes as they are tested with Mockito.

@InjectMocks will allow you to inject other spies and mocks into a class that requires services when it is being tested. The annotation will scan the class under test and automatically inject all available mocks and spies from within the test class. No more NullPointerExceptions when an unknown service is called!

Comment, like and subscribe for more content on Java and software engineering.

Рекомендации по теме
Комментарии
Автор

Very well explained. Exactly what I was looking. Thank you!!

semharweldegiorgis
Автор

Great Tutorial!
Two questions:

2) How to implement mocks if I have another custom logger class inside of BreakfastWaiter and DinnerWaiter classes? In that case, it will be a mock inside mock, Ive tried just mocking the BreakfastWaiter for example, but when I call the method in its CustomLogger class, it would throw a NullPointer. How to tackle that case?

amrutafolane
Автор

Can we also annotate the two breakfast and dinner services with @Autowired annotation? If we do not use constructor or setters ?

soumyadebbasu
Автор

I get a null pointer exception when I implement this. It says that " this.customerOrder is null ".

takalanimufamadi