Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strengthen tests for @QuarkusMain #45000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}
}
}
Loading