JUnit in this instance is a runner. This question is really about the difference between unit testing and integration testing, both of which can be implemented using JUnit as the surrounding execution framework.

There are many combinations of framework you could use. Some common ones:

  • JUnit + mockito for unit testing - where your REST API controller beans are wired up to lightweight/mocked dependencies and you execute the API through JAVA
  • JUnit + Cucumber + RESTAssured for integration testing - where you write a test fixture that expects to execute against a running server in order to exercise its API

There are points between these extremes too. You have to decide where your tests sit on the test pyramid. For highly permutational tests, you will want to write unit tests in order to be able to achieve the permutations easily and get speed of execution. If you're really trying to smoke test that your APIs are available, having already unit tested them, then you'll want to write a small number of integration tests.

In between the points of the spectrum there is a combination of mockito + the native test library of your service. For example, in Spring there's SpringTest and in Jersey there's the JerseyTest/Grizzly framwork. In these instances, a non-real http server is stood up to host your REST service and you test it by simulated REST calls to it, through the framework's client. This unit tests the HTTP marshalling layer as well as the first layer of REST controller code.

Answer from Ashley Frieze on Stack Overflow
Top answer
1 of 1
7

JUnit in this instance is a runner. This question is really about the difference between unit testing and integration testing, both of which can be implemented using JUnit as the surrounding execution framework.

There are many combinations of framework you could use. Some common ones:

  • JUnit + mockito for unit testing - where your REST API controller beans are wired up to lightweight/mocked dependencies and you execute the API through JAVA
  • JUnit + Cucumber + RESTAssured for integration testing - where you write a test fixture that expects to execute against a running server in order to exercise its API

There are points between these extremes too. You have to decide where your tests sit on the test pyramid. For highly permutational tests, you will want to write unit tests in order to be able to achieve the permutations easily and get speed of execution. If you're really trying to smoke test that your APIs are available, having already unit tested them, then you'll want to write a small number of integration tests.

In between the points of the spectrum there is a combination of mockito + the native test library of your service. For example, in Spring there's SpringTest and in Jersey there's the JerseyTest/Grizzly framwork. In these instances, a non-real http server is stood up to host your REST service and you test it by simulated REST calls to it, through the framework's client. This unit tests the HTTP marshalling layer as well as the first layer of REST controller code.

🌐
Jason.LeMauk
jasonlemauk.com › home › restassured, junit, and cucumber: a comparative guide
RestAssured, JUnit, and Cucumber: A Comparative Guide - Jason.LeMauk
March 10, 2024 - RestAssured: Primarily focused on API testing. JUnit: Versatile framework suitable for unit, integration, and even end-to-end testing.
🌐
Reddit
reddit.com › r/softwaretesting › rest assured framework with junit5
r/softwaretesting on Reddit: Rest Assured Framework with Junit5
July 11, 2022 -

In my new job I need to automate API testing, so far I have only done UI automation with selenium. I am a bit lost on where to start now.

I need to automate API testing of microservices. The tech stack I will have to use is Rest Assured with Junit 5. The first task is to validate the json schema of the API under test.

Does anyone know of a tutorial series or simialr that explains the framework setup? I need guidance on project structure and especially in combination with the necessity of using Junit5.

🌐
SourceForge
sourceforge.net › software › compare › JUnit-vs-REST-Assured
JUnit vs. REST Assured Comparison
Compare JUnit vs. REST Assured using this comparison chart. Compare price, features, and reviews of the software side-by-side to make the best choice for your business.
🌐
Frugal Testing
frugaltesting.com › blog › rest-assured-api-testing-tutorial-with-testng-and-junit
REST Assured API Testing Tutorial with TestNG and JUnit
October 28, 2024 - Here’s an example of a basic GET request using JUnit 5: import io.restassured.RestAssured; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class ApiTestWithJUnit { @Test public void validateGetRequest() { // Set the base URI RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; // Send GET request and extract the response Response response = RestAssured .given() .when().get("/posts/1") .then().statusCode(200) // Assert status code .extract().response(); // Extract the response // Use JSON path to assert the presence of 'id' int actualId = response.jsonPath().getInt("id"); Assertions.assertEquals(1, actualId, "Expected post ID (1) does not match actual ID (" + actualId + ")"); } }
🌐
QA Automation Expert
qaautomation.expert › 2023 › 10 › 12 › integration-of-rest-assured-with-junit5
Integration of REST Assured with JUnit5 – QA Automation Expert
July 20, 2024 - Last Updated On HOME In this tutorial, I’ll create a Test Framework for the testing of REST API using REST Assured and JUnit5 as the test framework. Table of Contents What is Rest Assured? What is JUnit5? Dependency List Detailed Step Description Download and Install Java Download and setup Eclipse IDE on the system Setup…
Find elsewhere
🌐
LinkedIn
linkedin.com › pulse › pros-cons-different-api-test-tools-restassured-craig-risi
The Pros and Cons of Different API Test Tools – RestAssured
August 10, 2023 - Powerful Validation: RestAssured ... response payloads. Integration with Testing Frameworks: It integrates well with popular Java testing frameworks like JUnit and ......
🌐
Aspiresys
blog.aspiresys.pl › strona główna › junit 5 and rest assured using extension api
JUnit 5 and Rest Assured using Extension API - Aspire Systems Poland Blog
January 29, 2020 - public class RestAssuredExtension implements BeforeAllCallback { @Override public void beforeAll(ExtensionContext context) throws Exception { RestAssured.port = 8080; RestAssured.baseURI = "http://example.com"; RestAssured.rootPath = "/api"; } } Setting common configuration properties has been moved into single place – Test Lifecycle Callback ·
🌐
Stack Overflow
stackoverflow.com › questions › 64276002 › i-need-help-setup-a-controller-test-using-spring-junit-5-mockito-rest-assured
I need help setup a controller test using Spring, Junit 5, Mockito, REST-Assured, and Lombok - Stack Overflow
Correction: @Before in UserControllerTest.java has been corrected to @BeforeEach as is correct for junit 5. Update: The use of Spring requires a careful use of imports, as noted in the REST-Assured docs. You should use: import static io.restassured.module.mockmvc.RestAssuredMockMvc.* import static io.restassured.module.mockmvc.matcher.RestAssuredMockMvcMatchers.* And avoid using: io.restassured.RestAssured.* io.restassured.matcher.RestAssuredMatchers.* As near as I can tell there is not error to tell you, that you are using the wrong import.
🌐
Guru99
guru99.com › home › software testing › rest assured api testing tutorial
REST Assured Tutorial for API Automation Testing (Example)
2 days ago - REST Assured pairs naturally with TestNG and JUnit for assertions, lifecycle hooks, and reporting.
🌐
Baeldung
baeldung.com › home › http client-side › a guide to rest-assured
A Guide to REST-assured | Baeldung
January 11, 2025 - RestAssured.reset(); Let’s see how we can measure the response time using the time() and timeIn() methods of the Response object: @Test public void whenMeasureResponseTime_thenOK() { Response response = RestAssured.get("/users/eugenp"); long timeInMS = response.time(); long timeInS = response.timeIn(TimeUnit.SECONDS); assertEquals(timeInS, timeInMS/1000); } Note that, in the above example: time() is used to get response time in milliseconds ·
🌐
Medium
medium.com › @bubu.tripathy › testing-restful-apis-with-rest-assured-6d245401deea
Testing RESTful APIs with REST-Assured | by Bubu Tripathy | Medium
May 9, 2023 - import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class APITest { @Test public void testJSONArray() { given() .when() .get("https://api.example.com/users") .then() .statusCode(200) .body("books", hasSize(3)) .body("books[0].title", equalTo("Book 1")) .body("books[1].author", containsString("Doe")); } } If you have a Spring Boot project and want to incorporate JUnit 5 and REST-Assured for testing your APIs, you can follow these steps to set up the necessary dependencies and configurations.
🌐
StackShare
stackshare.io › maven-io-rest-assured-rest-assured › alternatives
io.rest-assured:rest-assured Alternatives
5,782 stacks0 votes2 followersCompare io.rest-assured:rest-assured vs junit:junit →
🌐
ApyHub
apyhub.com › home
How to Use REST Assured for Advanced API Testing
import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; import org.junit.jupiter.api.Test; public class QuickGetTest { @Test public void validateUsersEndpoint() { given() .when() .get("/users") .then() .statusCode(200) .body("[0].name", equalTo("Leanne Graham")) .header("Content-Type", containsString("application/json")); } } This test validates the status code, response body, and headers in a concise chain pure efficiency.
🌐
Medium
paras301.medium.com › cucumber-bdd-testing-using-junit-framework-for-rest-apis-using-restassured-2a57f41fbaa1
Cucumber BDD testing using Junit Framework for Rest APIs using RestAssured | by Paras Bansal | Medium
April 17, 2022 - This post is similar to my previous post on using Cucumber for UI based testing and its automation with Gitlab CICD. The difference being here I’ll demonstrate how to test Rest APIs and automate them using Gitlab CICD. Also, this time we’ll use the long known Junit framework for our testing. Let’s touch a little bit on RestAssured.
🌐
Rest-assured
rest-assured.io
REST Assured
Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain. For example, if your HTTP server returns the following JSON at “http://localhost:8080/lotto/{id}”: ...
🌐
Libhunt
java.libhunt.com › compare-rest-assured-vs-junit
REST Assured vs JUnit - Awesome Java - LibHunt
Compare REST Assured and JUnit's popularity and activity. Categories: Testing. REST Assured is less popular than JUnit.