End-to-End (E2E) testing ensures that your application works as a whole by simulating real user workflows across the backend (Java) and frontend (React). This guide covers the tools, setup, and steps for implementing E2E testing.
For E2E testing in a Java-React stack, use tools that can interact with both the backend and frontend:
Frontend Testing Tools:
Backend Testing Tools:
Integration Tools:
Example (REST Assured test for an API):
import io.restassured.RestAssured; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; public class ApiTest { @Test public void testGetUser() { RestAssured.baseURI = "http://localhost:8080"; given() .when() .get("/users/1") .then() .statusCode(200) .body("name", equalTo("John Doe")); } }
Mock External Dependencies:
Containerize Backend:
FROM openjdk:11 COPY target/myapp.jar /app/myapp.jar ENTRYPOINT ["java", "-jar", "/app/myapp.jar"]
npm install cypress --save-dev
Create Cypress Tests:
describe('Login Page', () => { it('should log in successfully', () => { cy.visit('http://localhost:3000/login'); cy.get('input[name="username"]').type('admin'); cy.get('input[name="password"]').type('password123'); cy.get('button[type="submit"]').click(); cy.url().should('include', '/dashboard'); }); });
Mock APIs in Cypress:
cy.intercept('POST', '/api/login', { statusCode: 200, body: { token: 'fake-token' } });
Backend Test:
given() .contentType("application/json") .body("{ \"username\": \"admin\", \"password\": \"password123\" }") .when() .post("/login") .then() .statusCode(200) .body("token", notNullValue());
Frontend Test:
import io.restassured.RestAssured; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; public class ApiTest { @Test public void testGetUser() { RestAssured.baseURI = "http://localhost:8080"; given() .when() .get("/users/1") .then() .statusCode(200) .body("name", equalTo("John Doe")); } }
FROM openjdk:11 COPY target/myapp.jar /app/myapp.jar ENTRYPOINT ["java", "-jar", "/app/myapp.jar"]
Backend Tests in CI:
npm install cypress --save-dev
Frontend Tests in CI:
describe('Login Page', () => { it('should log in successfully', () => { cy.visit('http://localhost:3000/login'); cy.get('input[name="username"]').type('admin'); cy.get('input[name="password"]').type('password123'); cy.get('button[type="submit"]').click(); cy.url().should('include', '/dashboard'); }); });
Full Integration:
cy.intercept('POST', '/api/login', { statusCode: 200, body: { token: 'fake-token' } });
GitHub Actions for CI:
given() .contentType("application/json") .body("{ \"username\": \"admin\", \"password\": \"password123\" }") .when() .post("/login") .then() .statusCode(200) .body("token", notNullValue());
Allure for Java:
@Test public void testCreateAndRetrieveItem() { String itemJson = "{ \"name\": \"Test Item\" }"; // Create Item given() .contentType("application/json") .body(itemJson) .post("/createItem") .then() .statusCode(201); // Retrieve Items given() .get("/items") .then() .statusCode(200) .body("[0].name", equalTo("Test Item")); }
Cypress Dashboard:
describe('Item Management', () => { it('should display the newly created item', () => { cy.visit('http://localhost:3000/items'); cy.get('button#create-item').click(); cy.get('input[name="itemName"]').type('Test Item'); cy.get('button#save-item').click(); cy.contains('Test Item').should('exist'); }); });
This guide sets up a robust E2E testing framework for a Java and React application. Let me know if you need help implementing any specific part!
The above is the detailed content of End-To-End Testing for Java React Applications. For more information, please follow other related articles on the PHP Chinese website!