Skip to main content

Unit tests

Maven runs the unit tests automatically during the build. The library under /libs/test provides helpers shared by all modules.

A test class that relies on dependency injection starts with these annotations:

@QuarkusTest
@TestProfile(CustomTestProfile.class)
public class FeederTest {
}

@QuarkusTest​

@QuarkusTest starts the Quarkus application in a test environment, with all its components configured: databases, injection, services. Services and REST resources are tested as if the application were running, and beans can be injected straight into the test class.

@TestProfile​

@TestProfile activates a specific set of settings for a test class.

  • Change settings for the tests, for example to point at a different database or to enable stub services.
  • Isolate environments, so that test settings do not interfere with development or production ones.
  • Tailor scenarios, with different profiles for different kinds of test.

A profile is a class that implements io.quarkus.test.junit.QuarkusTestProfile:

public class CustomTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"quarkus.vertx.cluster.clustered", "false",
"openent.cwd", "."
// Override the Neo4j address to use a specific database
//"quarkus.neo4j.uri", "bolt://localhost:7687"
);
}
}
  • getConfigOverrides overrides configuration properties.
  • getEnabledAlternatives enables alternative beans for the tests.

Without @TestProfile, Quarkus uses its default profile, test, whose properties go in application-test.properties.

@RunOnVertxContext​

@RunOnVertxContext runs a test on the Vert.x event loop thread:

@Test
@RunOnVertxContext
public void testShouldImportAaf() {
}

JUnit 4 and JUnit 5​

Use JUnit 5 in new test classes: the @Test annotation must come from org.junit.jupiter.api, not from org.junit.

The two versions use different life-cycle annotations: a setup method annotated with @BeforeClass runs under JUnit 4, and one annotated with @BeforeAll under JUnit 5. IntelliJ IDEA detects which runner a class needs.