REST API to read entities


Further resources


You can find the source code for this CRUD application on GitHub: https://github.com/rieckpil/blog-tutorials/tree/master/demo-crud-application


For more testing deep-dive with Spring Boot using a real world application example (Java 14, Spring Boot, React, TypeScript, AWS, Keycloak, PostgreSQL), take a look at the Testing Spring Boot Applications Masterclass.

  • Hmmm, I would say, that we can do more in getBookWithUnknownShouldReturn404, because if you don’t have getBookById method in controller and you run the test, it doesn’t fail. But is it correct? It doesn’t fail because you don’t have the particular endpoint, so it must but 404, but not because book with given ID was not found. So I would also add something like .andExpect(status().reason(“Book with ID ’42’ was not found.”)), so it fails if particular endpoint is missing.

    • Hi Daniel,

      good point – that’s true. The current test can lead to a false positive in case the endpoint is entirely missing. Adding the additional expecting as you propose can help here. Otherwise, as we have the happy-path for the same URL inside this test class, we have an additional verification that this endpoint exists.

      Kind regards,
      Philip

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    Pen
    >