12 Comments

  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. Really great/simple article I have ever seen so far about springboot testing which covers lots of the topics

  3. 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!

  4. 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+ ?

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

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

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

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

  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.

Comments

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