Ui Testing
In this lesson, we'll test the newly implemented event creation functionality through the user interface.
Set Up the Environment
Before we can test our event creation functionality, we need to start our development environment:
- Start the Docker services by running:
docker compose up- Run Maven clean and compile to ensure all generated code is up to date:
mvn clean compile- Start the Spring Boot application
Fixing the Database Column Names
When starting the application, you might encounter an error related to reserved keywords in PostgreSQL.
To fix this, update the Event entity's column names:
@Column(name = "event_start")
private LocalDateTime start;
@Column(name = "event_end")
private LocalDateTime end;This change avoids using PostgreSQL's reserved keywords start and end as column names.
Creating Your First Event
Navigate to '/organizers' in your browser and click "Create an Event".
You'll need to:
- Log in with your organizer credentials
- Fill in the event details:
- Name (required)
- Venue (required)
- At least one ticket type with:
- Name
- Price
- Total available tickets
- Description (optional)
- Choose whether to publish the event or keep it as a draft
Verifying Event Creation
After creating an event, you can verify its creation in several ways:
- Check the HTTP response status (should be 201 Created)
- Review the response JSON to ensure all fields are correct
- Use Adminer (available at port 8888) to check the database tables:
- events table - contains the event details
- ticket_types table - contains the ticket configuration
- users table - contains the organizer information
Link Event and TicketType
When checking adminer we see that the ticket types's event_id field is null in the database.
We fix this by ensuring we set the event object on the TicketType class when we create them in the event service:
@Override
public Event createEvent(UUID organizerId, CreateEventRequest event) {
User organizer = userRepository.findById(organizerId)
.orElseThrow(() -> new UserNotFoundException(
String.format("User with ID '%s' not found", organizerId))
);
// eventToCreate needs to be moved up here
Event eventToCreate = new Event();
List<TicketType> ticketTypesToCreate = event.getTicketTypes().stream().map(
ticketType -> {
TicketType ticketTypeToCreate = new TicketType();
ticketTypeToCreate.setName(ticketType.getName());
ticketTypeToCreate.setPrice(ticketType.getPrice());
ticketTypeToCreate.setDescription(ticketType.getDescription());
ticketTypeToCreate.setTotalAvailable(ticketType.getTotalAvailable());
// 2. The next line needs to be added
ticketTypeToCreate.setEvent(eventToCreate);
return ticketTypeToCreate;
}).toList();
eventToCreate.setName(event.getName());
eventToCreate.setStart(event.getStart());
eventToCreate.setEnd(event.getEnd());
eventToCreate.setVenue(event.getVenue());
eventToCreate.setSalesStart(event.getSalesStart());
eventToCreate.setSalesEnd(event.getSalesEnd());
eventToCreate.setStatus(event.getStatus());
eventToCreate.setOrganizer(organizer);
eventToCreate.setTicketTypes(ticketTypesToCreate);
return eventRepository.save(eventToCreate);
}Summary
- We experienced an error when attempting to save an event in the database
startandendare reserved keywords in PostgreSQL- We updated the event object to no longer use the reserve keywords
- We have been able to successfully create an event