|
| 1 | +package io.quarkus.arc.test.config.staticinit; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | + |
| 6 | +import java.util.Optional; |
| 7 | + |
| 8 | +import jakarta.enterprise.context.ApplicationScoped; |
| 9 | +import jakarta.enterprise.context.Initialized; |
| 10 | +import jakarta.enterprise.event.Observes; |
| 11 | +import jakarta.inject.Singleton; |
| 12 | + |
| 13 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 14 | +import org.junit.jupiter.api.AfterAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 17 | + |
| 18 | +import io.quarkus.test.QuarkusUnitTest; |
| 19 | + |
| 20 | +public class StaticInitConfigInjectionMissingValueFailureTest { |
| 21 | + |
| 22 | + static final String PROPERTY_NAME = "static.init.missing.apfelstrudel"; |
| 23 | + |
| 24 | + @RegisterExtension |
| 25 | + static final QuarkusUnitTest config = new QuarkusUnitTest() |
| 26 | + .withApplicationRoot(root -> root |
| 27 | + .addClasses(StaticInitBean.class)) |
| 28 | + .assertException(t -> { |
| 29 | + assertThat(t).isInstanceOf(IllegalStateException.class) |
| 30 | + .hasMessageContainingAll( |
| 31 | + "A runtime config property value differs from the value that was injected during the static intialization phase", |
| 32 | + "the runtime value of '" + PROPERTY_NAME |
| 33 | + + "' is [gizmo] but the value [null] was injected into io.quarkus.arc.test.config.staticinit.StaticInitConfigInjectionMissingValueFailureTest$StaticInitBean#value"); |
| 34 | + }); |
| 35 | + |
| 36 | + @Test |
| 37 | + public void test() { |
| 38 | + fail(); |
| 39 | + } |
| 40 | + |
| 41 | + @Singleton |
| 42 | + public static class StaticInitBean { |
| 43 | + |
| 44 | + @ConfigProperty(name = PROPERTY_NAME) |
| 45 | + Optional<String> value; |
| 46 | + |
| 47 | + // bean is instantiated during STATIC_INIT |
| 48 | + void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) { |
| 49 | + System.setProperty(PROPERTY_NAME, "gizmo"); |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @AfterAll |
| 55 | + static void afterAll() { |
| 56 | + System.clearProperty(PROPERTY_NAME); |
| 57 | + } |
| 58 | +} |
0 commit comments