diff --git a/docs/src/main/asciidoc/command-mode-reference.adoc b/docs/src/main/asciidoc/command-mode-reference.adoc index 14f8f2f1831d0..074fe72adc894 100644 --- a/docs/src/main/asciidoc/command-mode-reference.adoc +++ b/docs/src/main/asciidoc/command-mode-reference.adoc @@ -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] ---- diff --git a/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/CdiBean.java b/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/CdiBean.java new file mode 100644 index 0000000000000..610105ab90ff8 --- /dev/null +++ b/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/CdiBean.java @@ -0,0 +1,5 @@ +package io.quarkus.it.testsupport.commandmode; + +public interface CdiBean { + String myMethod(); +} \ No newline at end of file diff --git a/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/DefaultBean.java b/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/DefaultBean.java new file mode 100644 index 0000000000000..0ffdc6de16578 --- /dev/null +++ b/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/DefaultBean.java @@ -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"; + } +} diff --git a/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/MainApp.java b/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/MainApp.java new file mode 100644 index 0000000000000..4557b5c5b033d --- /dev/null +++ b/integration-tests/test-extension/tests/src/main/java/io/quarkus/it/testsupport/commandmode/MainApp.java @@ -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; + } +} \ No newline at end of file diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/testsupport/commandmode/QuarkusMainTestWithTestProfileTestCase.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/testsupport/commandmode/QuarkusMainTestWithTestProfileTestCase.java new file mode 100644 index 0000000000000..1e69dc60e5012 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/testsupport/commandmode/QuarkusMainTestWithTestProfileTestCase.java @@ -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 getConfigOverrides() { + return Map.of("quarkus.package.main-class", "test"); + } + + @Override + public Set> getEnabledAlternatives() { + return Set.of(MockedCdiBean.class); + } + } + + @Alternative + @Singleton + public static class MockedCdiBean implements CdiBean { + + @Override + public String myMethod() { + return "mocked value"; + } + } +} \ No newline at end of file