Skip to content

Latest commit

 

History

History
98 lines (87 loc) · 3.17 KB

guide-quarkus-testing.asciidoc

File metadata and controls

98 lines (87 loc) · 3.17 KB

Testing

Configuration

Quarkus relies on JUnit for testing.

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-junit5</artifactId>
    <scope>test</scope>
</dependency>

The extension quarkus-junit5 provides the @QuarkusTest annotation that controls the testing framework. The tests are run by default on port 8081. To change it, modify the application.properties correspondingly:

quarkus.http.test-port=8083
quarkus.http.test-ssl-port=8446

Quarkus supports injecting scoped CDI beans into your tests via @Inject annotation for e.g. unit testing or beans testing.

Mocking CDI beans

The io.quarkus.test.junit.QuarkusMock class can be used to temporarily mock out any normal scoped bean (e.g. @ApplicationScoped, @RequestScoped etc, basically every scope except @Singleton and @Dependent). If you use this method in a @BeforeAll, the mock will take effect for all tests on the current class, while if you use this in a test method, the mock will only take effect there. An example is given below:

@QuarkusTest
public class MockTestCase {
    @Inject
    MockableBean1 mockableBean1;

    @Inject
    MockableBean2 mockableBean2;

    @BeforeAll
    public static void setup() {
        MockableBean1 mock = Mockito.mock(MockableBean1.class);
        QuarkusMock.installMockForType(mock, MockableBean1.class);
    }

    @Test
    public void testPerTestMock() {
        QuarkusMock.installMockForInstance(new MockClass(), mockableBean2);
    }
}

Quarkus also allows users to effortlessly take advantage of Mockito.

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-junit5-mockito</artifactId>
    <scope>test</scope>
</dependency>

The example above can be simplified like so:

@QuarkusTest
public class MockTestCase {
    @InjectMock
    MockableBean1 mockableBean1;

    @InjectMock
    MockableBean2 mockableBean2;
}

A RestClient can also be easily mocked with @InjectMock and @RestClient annotations.

@Path("/")
@ApplicationScoped
@RegisterRestClient
public interface DemoRestClient {
}

@QuarkusTest
public class MockTestCase {
    @InjectMock
    @RestClient
    DemoRestClient demoRestClient;
}

Test profiles

Quarkus supports testing different configurations with test profiles. A test profile has to implement the QuarkusTestProfile . To write a profile, please follow this guide.

Continuous testing

By design, Quarkus enables test-driven development. It detects affected tests as changes are made and automatically rerun them in background. As that, it gives developer instant feedback. To use continuous testing, execute the following command:

mvn quarkus:dev

For more details, see here.

Native testing

It’s possible to test native executables using @NativeImageTest (which might be replaced by @QuarkusIntergrationTest in the future). For more, see here.