16 Comments

  1. Thank you for efforts, you have created a really outstanding article with simple explanations for the not exactly simple theme. Finally, I have sorted out how to test Controller layer in Spring application and got to know about different approaches.

  2. Thanks a lot for this excellent article, i have seen too many tutorial and examples where they mixed up integration and unit testing and they don’t really know what they are doing, at some point was getting confused too.
    Excelent clarification

  3. Assuming you are using Junit 4, they only changed the packaged where it was placed.

    Before (deprecated) org.mockito.runners.MockitoJUnitRunner
    Current (use this) : org.mockito.junit.MockitoJUnitRunner

  4. Excellent Job. Many Thanks. Strategy 1 and 4 are working for me, Strategy 2 and 3 are not working, it is possible that I use Spring Boot 2 and Authentication as well. Just a question, in Spring Boot 2, Strategy 1 (@RunWith(MockitoJUnitRunner.class): MocktioJUnitRunner.class is deprecated, what is new Class to replace it?

    1. Author

      Thanks for your feedback! I’ll update that part soon, didn’t find time for it so far.

  5. i think Strategy 4 is Integration Tests, so we should use real database , not mock repository.

    1. Author

      It is indeed an Integration Test since we’re testing how the different layers or components integrate with each other, but the name doesn’t imply that we should use a real database, that depends on what we want to test.

  6. Excellent explanation on different options, thanks!

  7. Author

    I don’t think that the problem is related to returning a list instead of a single object. It’s difficult to analyze what’s exactly going on there without the whole context, but you can try replacing your given statement by

    just to discard problems related to equality checks of mock methods.

  8. I am basically trying to do the same thing as canRetrieveByNameWhenExists() but instead of returning a single object my controller method returns a List. The response returns a 200 but the content is empty. If I remove the given statement and the 2nd assert, the test passes. I’m wondering why this doesn’t work the same as a method that returns a single object?

  9. Regarding the Standalone approach, do you have an example where given willReturn a List? I am having problems making this work and cannot find good documentation. I know this article is a year old but I would appreciate any help. Thanks.

    1. Author

      I wrote another post recently focused on BDD Mockito and assertions with AssertJ in which I use examples of given(someMethod).willReturn(list):
      BDD Unit Tests with BDDMockito and AssertJ
      If that doesn’t solve your question, just point me to your code (GitHub for example) and I can have a look.

  10. Excellent article! The best article I’ve found about testing springboot

  11. I really appreciate the depth of this article! Thanks

  12. This guide is simply Awesome! Thanks for such great work explaining all the options to test controllers and differences between them. :+1

Comments

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