Spring boot REST application testing approach

Harshana

I have a Spring boot + REST application. When I need to write unit testing, should I directly invoke the service beans or call the rest controller? If I invoke the rest controller directly, I have to use RestTemplate and invoke the rest api as a client, right?

What would be the best and required practice?

If I invoke the service beans directly it will result in less code coverage because controller methods code will be not covered. Is that acceptable?

Taylor

Hmm, this is a complex question but I'll answer as best I can. A lot of this will depend on you/your organization's risk tolerance and how much time they want to invest in tests. I believe in a lot of testing, but there is such a thing as too much.

A unit test tests the unit of code. Great, but what's a unit? This article is a pretty good discussion: http://martinfowler.com/bliki/UnitTest.html but a unit is basically the smallest testable part of your application.

Much literature (e.g. https://www.amazon.ca/Continuous-Delivery-Reliable-Deployment-Automation/dp/0321601912/ ) describes multiple phases of testing including unit tests which are very low level and mock externalities such as DBs or file systems or remote systems, and "api acceptance tests" (sometimes called integration tests although this is a vague term that can mean other things). This latter type fires up a test instance of your application, invokes APIs and asserts on responses.

The short answer is as follows: for unit tests, focus on the units (probably services or more granular), but the other set of tests you describe, wherein the test behaves like a client and invokes your api, are worthwhile too. My suggestion: do both, but don't call both unit tests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

testing spring boot rest application with restAssured

From Dev

Slow unit testing in spring-boot application

From Dev

Unit testing Spring Boot application endpoints

From Dev

NullPointerException while testing Spring boot camel application

From Dev

How to use Firebase with Spring boot REST Application?

From Dev

Connecting MySQL to a Spring Boot REST Application

From Dev

How to use Firebase with Spring boot REST Application?

From Dev

Handling gzipped requests in a Spring Boot REST application

From Dev

spring-boot integration testing using rest-assured

From Dev

Should I mix integration testing and rest documentation with Spring Boot?

From Dev

How to configure EntityManagerFactoryBuilder bean when testing Spring Boot Batch application?

From Dev

Integration testing of multi-war application in Spring Boot

From Dev

Spring Boot maven multimodule project - unit testing (application context)

From Dev

What is the best approach for using SSL if you are conterized Spring Boot application in docker

From Dev

Spring Boot 1.4 Testing

From Dev

Best approach for securing REST services and a web application

From Dev

Error while Handling 404 error for Spring Boot REST application

From Dev

Getting "No message available" error with Spring Boot + REST application

From Dev

Using Camel with Spring-boot to build a REST Application

From Dev

Securing JSON-PATCH paths in Spring Boot Data Rest application

From Dev

Can Spring Boot application have separate security for REST APIs?

From Dev

Using Camel with Spring-boot to build a REST Application

From Dev

Accept empty String for Enum in REST calls in Spring Boot application

From Dev

Spring Test returning 404 instead of 200 in testing Rest API in Spring Boot

From Dev

Better approach to return HttpStatus code Spring REST

From Dev

Testing a spring mvc rest controller

From Dev

Spring boot multi module testing

From Dev

Spring Boot BDD Testing with serenity

From Dev

Spring Boot setup security for testing

Related Related

  1. 1

    testing spring boot rest application with restAssured

  2. 2

    Slow unit testing in spring-boot application

  3. 3

    Unit testing Spring Boot application endpoints

  4. 4

    NullPointerException while testing Spring boot camel application

  5. 5

    How to use Firebase with Spring boot REST Application?

  6. 6

    Connecting MySQL to a Spring Boot REST Application

  7. 7

    How to use Firebase with Spring boot REST Application?

  8. 8

    Handling gzipped requests in a Spring Boot REST application

  9. 9

    spring-boot integration testing using rest-assured

  10. 10

    Should I mix integration testing and rest documentation with Spring Boot?

  11. 11

    How to configure EntityManagerFactoryBuilder bean when testing Spring Boot Batch application?

  12. 12

    Integration testing of multi-war application in Spring Boot

  13. 13

    Spring Boot maven multimodule project - unit testing (application context)

  14. 14

    What is the best approach for using SSL if you are conterized Spring Boot application in docker

  15. 15

    Spring Boot 1.4 Testing

  16. 16

    Best approach for securing REST services and a web application

  17. 17

    Error while Handling 404 error for Spring Boot REST application

  18. 18

    Getting "No message available" error with Spring Boot + REST application

  19. 19

    Using Camel with Spring-boot to build a REST Application

  20. 20

    Securing JSON-PATCH paths in Spring Boot Data Rest application

  21. 21

    Can Spring Boot application have separate security for REST APIs?

  22. 22

    Using Camel with Spring-boot to build a REST Application

  23. 23

    Accept empty String for Enum in REST calls in Spring Boot application

  24. 24

    Spring Test returning 404 instead of 200 in testing Rest API in Spring Boot

  25. 25

    Better approach to return HttpStatus code Spring REST

  26. 26

    Testing a spring mvc rest controller

  27. 27

    Spring boot multi module testing

  28. 28

    Spring Boot BDD Testing with serenity

  29. 29

    Spring Boot setup security for testing

HotTag

Archive