Project setup

Light Air v. 4 requires Java 17.

POM

Maven dependencies

Add JUnit dependency to your POM. Use the version used by Light Air itself.

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>6.1.3</version>
  <scope>test</scope>
</dependency>

Alternatively, you can use TestNG instead of JUnit:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.12.0</version>
  <scope>test</scope>
</dependency>

Add Light Air dependency to your POM:

<dependency>
  <groupId>net.sf.lightair</groupId>
  <artifactId>light-air</artifactId>
  <version>4.1.0</version>
  <scope>test</scope>
</dependency>

Have an Slf4J runtime compatible with Slf4J used by Light Air. Logback is recommended.

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.6.6</version>
  <scope>test</scope>
</dependency>

Test resources

The XML dataset files are expected to be next to the test class, in the same package. This way you are not required to chase them all over the project or switch regularly between the test class directory and test resources directory.

To support this properly in Maven, use the following:

<build>
  <testResources>
    <testResource>
      <directory>src/test/resources</directory>
    </testResource>
    <testResource>
      <directory>src/test/java</directory>
      <includes>
        <include>**</include>
      </includes>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </testResource>
  </testResources>
</build>

Integration tests profile

As Light Air tests require the application to be running, it is typically not desirable that they be run as unit tests in the Maven test lifecycle phase.

A way to approach this issue is to name the tests with “*IT.java” suffix (as opposed to “*Test.java”). This excludes them from the Maven test lifecycle phase and makes sure such tests are picked up by the Maven Failsafe plugin, which is designed to run this type of tests. Next, define a profile to run the Light Air tests:

<profiles>
  <profile>
    <id>it</id>
    <build>
      <plugins>
        <plugin>
          <!-- Spring Boot example. -->
          <!-- Replace with your way of starting your app. -->
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <executions>
            <execution>
              <phase>pre-integration-test</phase>
              <goals>
                <goal>start</goal>
              </goals>
            </execution>
            <execution>
              <phase>post-integration-test</phase>
              <goals>
                <goal>stop</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

The example above also includes a template for Spring Boot to start and stop the application. You would replace that with a configuration corresponding to your particular way of doing that.

Now, you can start your app and execute all Light Air integration tests by issuing:

mvn clean install -Pit

where -Pit activates the “integration tests” profile.

After that, you can open any test in your IDE and run it as an ordinary unit test to take advantage of IDE support.

Properties file

Create light-air.properties file in src/test/resources with the following content (example for the H2 database):

# Database connection
database.driverClassName=org.h2.Driver
database.connectionUrl=jdbc:h2:~/data/my-project/db;AUTO_SERVER=TRUE;DB_CLOSE_ON_EXIT=FALSE
database.userName=sa
database.password=
database.schema=PUBLIC

# Set the maximum allowable difference of temporal column values in verification
# This value should be greater than the maximum duration of any test case (on any environment)
time.difference.limit.millis=5000

Read more about configuring Light Air.

Be aware that Light Air tests must connect to the same database instance as the application running within the application server.
Therefore, you are looking to access the database in a “server mode”, or via “TCP connection”, as opposed to “in-memory mode” or “embedded mode”.

The same connection URL must be configured for the data source of the application in the application server.

Generate XSD files

Use the LightAir Maven plugin (or call LightAirApi.generateXsd) to generate XSD files from the database.

Logback test configuration file

If you use Logback as the Slf4J runtime, create a file named logback-test.xml in src/test/resources with content recommended on the Logback site.

This file lets you manage logging in tests, including log output from Light Air.

(Note: When another dependency overrides Slf4J API with a conflicting incompatible version, you may have to override it explicitly in POM.)

Sample project

There is a sample project demonstrating using Light Air at https://github.com/ivos/lightair-spring-sample.

Feel free to copy & paste code from there.

You may also get some inspiration about writing Light Air tests.

Next: Write a test