Home » Java » Enterprise Java » Testing with Mockito

About Eidher Julian

Eidher Julian
Eidher Julian is a Systems Engineer and Software Engineering Specialist with 13+ years of experience as a Java developer. He is an Oracle Certified Associate and SOA Certified Architect.

Testing with Mockito

1) Register MockitoExtension

class ObjectTest {
    static final Long ID = 1L;

2) Create the mock

    private ObjectRepo mockRepo;

3) Inject the mock

    private ObjectService objectService;

    void whenfindByIdThenReturnResult() {
        var objectDAO = new ObjectDAO();

4) Define the behavior


5) Test

var result = ObjectService.findById(ID);

6) Verify

verify(mockRepo, times(1)).findById(any(Long.class));

7) Validate

                () -> assertNotNull(result),
                () -> assertEquals(objectDAO.getId(), result.getId())
Published on Java Code Geeks with permission by Eidher Julian, partner at our JCG program. See the original article here: Testing with Mockito

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
1 month ago

Thanks a lot for this algorithm, that is really useful for me