JUnit 5 Mockito CookBook v1.0

NGU
20 min readAug 17, 2023

Let's find something you may need to use and leave with many takeaways.

Why use Mock?

Mock can be understood as creating a fake object or mocking up an object to be used in a test environment to replace the real object
* Verify how certain methods of the object are called, how many times they are called, what the parameters are, etc.
* Define the behavior of the object to specify the return result or to specify a particular action.

In which cases do I need to use a Mock instead of a real object?

* The behavior of real objects is uncertain (ex: current time or temperature)
* Real objects are hard to build
* The behavior of the real object is hard to trigger (ex: network error, network delay)
* Real objects are slow (ex: need to initialize data first)
* Real objects are user interfaces
* Real objects use a callback mechanism
* Real objects may also not exist
* Real objects may also not exist * Real objects may contain information and methods that cannot be used as tests

Ok.let go through these test tools.

Annotations

@Mock:

--

--

NGU

Microservices. Cloud Native. Architecture. Record Technology, Sports, Life. A picture is worth a thousand words