First Cypress Test

Let's write our first Cypress test.

Pre-requisites

A good unit or integration test needs to start from a well-known situation.

For a smooth testing process, we can expose certain REST API endpoints that will put the database in a well-known state. This could be empty, with a few test users, or with many users to test pagination, etc.

Let’s start by adding IntegrationTestController to src/main/java/test:

  • Create a @RestController to call the endpoints from the Cypress tests.

  • All endpoints will be served at /api/integration-test.

  • These endpoints should only be started when running as a test. This mustn’t be exposed when running on production as it wipes the complete database.

  • Inject the UserService in order to create the default users.

  • Add an endpoint /reset-db so the Cypress test can do a POST call on it.

  • Create a user with the role USER and Administrator, respectively.

Get hands-on with 1200+ tech skills courses.