1. Probably one of the best posts out there that really explains server side testing in a clear and detailed way. Thanks for the content, keep up the good work.

  2. thank you, it is very helpful.

  3. Really great/simple article I have ever seen so far about springboot testing which covers lots of the topics

  4. Really good and thanks for knowledge sharing

  5. Hi there – many thanks for this brilliant article – I’m trying to follow Strategy 1 (Standalone) and in my case I have a controller which persist data to db via a service (and the service uses a repository ) – the controller retrieves a value from the persisted data (like primary key ) and injects in into the response header.

    So two things I see here I need to verify
    1. Is the 201 (HTTPStatus.created)
    2. verify header contains the primary key

    I,m trying to write the test to focus on on the controller but I find myself digging into the service and repository – how would you write this ? could you please provide an example ?

    Thanks in advance !

    1. Author

      You could mock the service layer to return a dummy value. The controller is injecting the header and mapping the response code, that’s the behavior you want to verify in this case.
      Then, make sure to write separate unit tests to validate the logic in the service layer.
      I hope it helps, otherwise let me know if you have the code in a public repo and I can have a quick look…
      Thanks for the feedback on the article!

  6. Thanks for this great tutorial, I learned a lot. The examples use JUnit 4 though, would you consider updating them to JUnit5 / Spring Boot 2+ ?

  7. NOW I understand ! Simply a great guide

  8. Simply excellent! Thank you!
    Never seen such a clear description about the different ways testing spring rest applications.

  9. Thanks for the guide.
    Which strategy does this code belongs to: MockMvcBuilders.webAppContextSetup? Is this 2nd?

    1. Author

      Yes, that’s an alternative way of setting it up. You may need it if you want, for instance, to integrate Spring Security in your tests.

  10. Excellent article. Thanks for sharing your knowledge with us.

  11. 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.

  12. 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

  13. 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

  14. 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.

  15. 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.

  16. Excellent explanation on different options, thanks!

  17. 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.

  18. 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?

  19. 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.

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

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

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


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