Integration Testing
In an earlier section of this tutorial we looked at unit testing, but integration tests are at least as important, probably more so, as they exercise the entire application from an end-users perspective, rather than an individual part.
Integration tests are not written using Selenium or similar, so avoid the fragility and maintenance effort that such tests often entail. Instead, the framework provides the WrapperFactory domain service which simulates the user interface in a type-safe way.
Ex 9.1: Testing bookVisit using an integtest
In this exercise we’ll test the bookVisit
mixin action (on Pet
entity).
Tasks
-
in the
pom.xml
of the visits module, add the following dependency:module-visits/pom.xml<dependency> <groupId>org.apache.isis.mavendeps</groupId> <artifactId>isis-mavendeps-integtests</artifactId> <type>pom</type> <scope>test</scope> </dependency>
-
add an abstract class
VisitsModuleIntegTestAbstract
for thevisits
module, for other integ tests to subclass:VisitsModuleIntegTestAbstract.java@SpringBootTest( classes = VisitsModuleIntegTestAbstract.TestApp.class ) @ActiveProfiles("test") public abstract class VisitsModuleIntegTestAbstract extends IsisIntegrationTestAbstractWithFixtures { @SpringBootConfiguration @EnableAutoConfiguration @Import({ IsisModuleCoreRuntimeServices.class, IsisModuleSecurityBypass.class, IsisModulePersistenceJpaEclipselink.class, IsisModuleTestingFixturesApplib.class, VisitsModule.class }) @PropertySources({ @PropertySource(IsisPresets.H2InMemory_withUniqueSchema), @PropertySource(IsisPresets.UseLog4j2Test), }) public static class TestApp { } }
-
also update the
application-test.yml
file for thevisits
module, to ensure that the database schemas for both modules are created:module-visits/src/test/resources/application-test.ymlisis: persistence: schema: auto-create-schemas: pets,visits
-
add a class
Bootstrap_IntegTest
integration test, inheriting from the `VisitsModuleIntegTestAbstract:Bootstrap_IntegTest.javapublic class Bootstrap_IntegTest extends VisitsModuleIntegTestAbstract { @Test public void checks_can_bootstrap() {} }
Make sure this test runs and passes in both the IDE and using "mvn clean install".
Now we can write our actual test:
-
Now add a class
Pet_bookVisit_IntegTest
integration test, also inheriting from the `VisitsModuleIntegTestAbstract:Pet_bookVisit_IntegTest.javapublic class Pet_bookVisit_IntegTest extends VisitsModuleIntegTestAbstract { @BeforeEach void setup() { fixtureScripts.run(new Pet_persona.PersistAll()); (1) } @Test public void happy_case() { // given Pet somePet = fakeDataService.enums().anyOf(Pet_persona.class) (2) .findUsing(serviceRegistry); List<Visit> before = visitRepository.findByPetOrderByVisitAtDesc(somePet); // when LocalDateTime visitAt = clockService.getClock().nowAsLocalDateTime() (3) .plusDays(fakeDataService.ints().between(1, 3)); String reason = fakeDataService.strings().upper(40); (3) wrapMixin(Pet_bookVisit.class, somePet).act(visitAt, reason); (4) // then List<Visit> after = visitRepository.findByPetOrderByVisitAtDesc(somePet); after.removeAll(before); assertThat(after).hasSize(1); (5) Visit visit = after.get(0); assertThat(visit.getPet()).isSameAs(somePet); (6) assertThat(visit.getVisitAt()).isEqualTo(visitAt); (6) assertThat(visit.getReason()).isEqualTo(reason); (6) } @Inject FakeDataService fakeDataService; @Inject VisitRepository visitRepository; @Inject ClockService clockService; }
1 uses same fixture script used for prototyping to set up Pet
s and theirPetOwner
s.2 uses the FakeDataService to select a Pet
persona at random and uses that person to look up the corresponding domain object.3 sets up some randomised but valid argument values 4 invokes the action, using the WrapperFactory to simulate the UI 5 asserts that one new Visit
has been created for thePet
.6 asserts that the state of this new Visit
is correctRun the test and check that it passes.
-
write an error scenario which checks that a reason has been provided:
Pet_bookVisit_IntegTest.java@Test public void reason_is_required() { // given Pet somePet = fakeDataService.enums().anyOf(Pet_persona.class) .findUsing(serviceRegistry); List<Visit> before = visitRepository.findByPetOrderByVisitAtDesc(somePet); // when, then LocalDateTime visitAt = clockService.getClock().nowAsLocalDateTime() .plusDays(fakeDataService.ints().between(1, 3)); assertThatThrownBy(() -> wrapMixin(Pet_bookVisit.class, somePet).act(visitAt, null) ) .isInstanceOf(InvalidException.class) .hasMessage("'Reason' is mandatory"); }
-
write an error scenario which checks that the
visitAt
date cannot be in the past:Pet_bookVisit_IntegTest.java@Test public void cannot_book_in_the_past() { // given Pet somePet = fakeDataService.enums().anyOf(Pet_persona.class) .findUsing(serviceRegistry); List<Visit> before = visitRepository.findByPetOrderByVisitAtDesc(somePet); // when, then LocalDateTime visitAt = clockService.getClock().nowAsLocalDateTime(); String reason = fakeDataService.strings().upper(40); assertThatThrownBy(() -> wrapMixin(Pet_bookVisit.class, somePet).act(visitAt, reason) ) .isInstanceOf(InvalidException.class) .hasMessage("Must be in the future"); }
*