Skip to content

Commit

Permalink
Add tests for simple @QuarkusMain case
Browse files Browse the repository at this point in the history
I also slightly clarified the wording on the docs.

Co-Authored-By: Ladislav Thon <ladicek@gmail.com>
  • Loading branch information
holly-cummins and Ladicek committed Dec 12, 2024
1 parent 412e41a commit f8757d6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/command-mode-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ Consequently, mocking CDI beans with `QuarkusMock` or `@InjectMock` is not suppo

It is possible to mock CDI beans by leveraging xref:getting-started-testing.adoc#testing_different_profiles[test profiles] though.

For instance, in the following test, the singleton `CdiBean1` will be mocked by `MockedCdiBean1`:
For instance, in the following test, the launched application would receive a mocked singleton `CdiBean1`. The implementation `MockedCdiBean1` is provided by the test:

[source,java]
----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.it.testsupport.commandmode;

public interface CdiBean {
String myMethod();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.it.testsupport.commandmode;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class DefaultBean implements CdiBean {
@Override
public String myMethod() {
return "default bean";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.it.testsupport.commandmode;

import jakarta.inject.Inject;

import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;

/*
* Because this app co-exists in a module with a QuarkusIntegrationTest, it needs to not be on the default path.
* Otherwise, this application is executed by the QuarkusIntegrationTest and exits early, causing test failures elsewhere.
*/
@QuarkusMain(name = "test")
public class MainApp implements QuarkusApplication {

@Inject
CdiBean myBean;

@Override
public int run(String... args) throws Exception {
System.out.println("The bean is " + myBean.myMethod());
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.it.testsupport.commandmode;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;
import java.util.Set;

import jakarta.enterprise.inject.Alternative;
import jakarta.inject.Singleton;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.quarkus.test.junit.main.Launch;
import io.quarkus.test.junit.main.LaunchResult;
import io.quarkus.test.junit.main.QuarkusMainTest;

@QuarkusMainTest
@TestProfile(QuarkusMainTestWithTestProfileTestCase.MyTestProfile.class)
public class QuarkusMainTestWithTestProfileTestCase {

@Test
@Launch(value = {})
public void testLaunchCommand(LaunchResult result) {
assertThat(result.getOutput())
.contains("The bean is mocked value");
}

public static class MyTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Map.of("quarkus.package.main-class", "test");
}

@Override
public Set<Class<?>> getEnabledAlternatives() {
return Set.of(MockedCdiBean.class);
}
}

@Alternative
@Singleton
public static class MockedCdiBean implements CdiBean {

@Override
public String myMethod() {
return "mocked value";
}
}
}

0 comments on commit f8757d6

Please sign in to comment.