-
In a fresh Spring Boot project, there is already a single test class
EventRegistrationApplicationTests
in the src/test/java folder that looks like the following:package ca.mcgill.ecse321.eventregistration; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class EventRegistrationApplicationTests { @Test void contextLoads() { } }
-
Run this test that checks if the application can successfully load by right clicking on the class → Run as… → JUnit test
ImportantYou need to set the SPRING_DATASOURCE_URL
for the test run configuration as well if you use an environment variable to set datasource URL (see earlier). -
Add a new test class
ca.mcgill.ecse321.eventregistration.dao.TestEventRegistrationPersistence
and implement tests for the persistencepackage ca.mcgill.ecse321.eventregistration.dao; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.sql.Date; import java.sql.Time; import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; import ca.mcgill.ecse321.eventregistration.model.Event; import ca.mcgill.ecse321.eventregistration.model.Person; import ca.mcgill.ecse321.eventregistration.model.Registration; @ExtendWith(SpringExtension.class) @SpringBootTest public class TestEventRegistrationPersistence { @Autowired private PersonRepository personRepository; @Autowired private EventRepository eventRepository; @Autowired private RegistrationRepository registrationRepository; @AfterEach public void clearDatabase() { // Fisrt, we clear registrations to avoid exceptions due to inconsistencies registrationRepository.deleteAll(); // Then we can clear the other tables personRepository.deleteAll(); eventRepository.deleteAll(); } @Test public void testPersistAndLoadPerson() { String name = "TestPerson"; // First example for object save/load Person person = new Person(); // First example for attribute save/load person.setName(name); personRepository.save(person); person = null; person = personRepository.findPersonByName(name); assertNotNull(person); assertEquals(name, person.getName()); } @Test public void testPersistAndLoadEvent() { String name = "ECSE321 Tutorial"; Date date = java.sql.Date.valueOf(LocalDate.of(2020, Month.JANUARY, 31)); Time startTime = java.sql.Time.valueOf(LocalTime.of(11, 35)); Time endTime = java.sql.Time.valueOf(LocalTime.of(13, 25)); Event event = new Event(); event.setName(name); event.setDate(date); event.setStartTime(startTime); event.setEndTime(endTime); eventRepository.save(event); event = null; event = eventRepository.findEventByName(name); assertNotNull(event); assertEquals(name, event.getName()); assertEquals(date, event.getDate()); assertEquals(startTime, event.getStartTime()); assertEquals(endTime, event.getEndTime()); } @Test public void testPersistAndLoadRegistration() { String personName = "TestPerson"; Person person = new Person(); person.setName(personName); personRepository.save(person); String eventName = "ECSE321 Tutorial"; Date date = java.sql.Date.valueOf(LocalDate.of(2020, Month.JANUARY, 31)); Time startTime = java.sql.Time.valueOf(LocalTime.of(11, 35)); Time endTime = java.sql.Time.valueOf(LocalTime.of(13, 25)); Event event = new Event(); event.setName(eventName); event.setDate(date); event.setStartTime(startTime); event.setEndTime(endTime); eventRepository.save(event); Registration reg = new Registration(); int regId = 1; // First example for reference save/load reg.setId(regId); reg.setPerson(person); reg.setEvent(event); registrationRepository.save(reg); reg = null; reg = registrationRepository.findByPersonAndEvent(person, event); assertNotNull(reg); assertEquals(regId, reg.getId()); // Comparing by keys assertEquals(person.getName(), reg.getPerson().getName()); assertEquals(event.getName(), reg.getEvent().getName()); } }
-
In the end, you should have the given below structure in terms of packages.
-
Run this test suite by right clicking on the class → Run as… → JUnit test. Again, don’t forget to set the
SPRING_DATASOURCE_URL
value for the run configuration.