Skip to content

SDA Commons Server Testing

javadoc

The module sda-commons-server-testing is the base module to add unit and integrations test for applications in the SDA SE infrastructure. It provides JUnit test extensions that are helpful in integration tests.

Add the module with test scope:

1
  testCompile "org.sdase.commons:sda-commons-server-testing"
In case you want to use JUnit 5 you also have to activate it in your build.gradle:
1
2
3
4
5
6
7
8
  test {
    useJUnitPlatform()
  }

  // in case you use the integrationTest plugin:
  integrationTest {
    useJUnitPlatform()
  }

Provided Assertions

GoldenFileAssertions

Special assertions for Path objects to check if a file matches the expected contents and updates them if needed. These assertions are helpful to check if certain files are stored in the repository (like OpenAPI or AsyncApi).

Use this assertion if you want to conveniently store the latest copy of a file in your repository, and let the CI fail if an update has not been committed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class MyTestIT {
  @Test
  public void shouldHaveSameFileInRepository() throws IOException {
    // get expected content from a generator or rest endpoint
    String expected = ...; 

    // get a path to the file that should be checked
    Path filePath = Paths.get("my-file.yaml");

    // assert the file and update the file afterwards
    GoldenFileAssertions.assertThat(filePath)
        .hasContentAndUpdateGolden(expected);
  }
}

There is also a assertThat(...).hasYamlContentAndUpdateGolden(...) variant that interprets the content as YAML or JSON and ignores the order of keys. If possible, prefer the other variant since the written content should always be reproducible. Note that the AsyncAPI and OpenAPI generations export reproducible content.

SystemPropertyClassExtension

The SystemPropertyClassExtension allows for overriding or unsetting system properties for (integration) tests and resets them to their original value when the tests have finished.

To use the extension, register it to your test class via the JUnit5 @RegisterExtension:

1
2
3
4
5
6
@RegisterExtension
public SystemPropertyClassExtension PROP =
  new SystemPropertyClassExtension()
    .setProperty(PROP_TO_SET, VALUE)
    .setProperty(PROP_TO_SET_SUPPLIER, () -> VALUE)
    .unsetProperty(PROP_TO_UNSET);

Provided JUnit 5 Extensions

SystemPropertyClassExtension

This module provides the SystemPropertyClassExtension, a JUnit5 test extension to set and unset system properties before running an integration test.

To use the extension register it to your test class via the JUnit5 @RegisterExtension:

1
2
3
4
5
6
@RegisterExtension
static final SystemPropertyClassExtension PROP = 
  new SystemPropertyClassExtension()
    .setProperty(PROP_TO_SET, VALUE)
    .setProperty(PROP_TO_SET_SUPPLIER, () -> VALUE)
    .unsetProperty(PROP_TO_UNSET);

Set and overwritten values will be reset to their original value once the test has finished.

JUnit Pioneer

For JUnit 5 we will not supply a replacement for the EnvironmentRule and DropwizardRuleHelper.

  • The EnvironmentRule can be replaced with JUnit Pioneer capabilities. Anyway please read the warning above for overriding environment variables.
  • Use DropwizardAppExtension directly as a replacement for the DropwizardRuleHelper.