-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide easy way for QuarkusTestResourceLifecycleManager impls to inj…
…ect into test Resolves: #18698
- Loading branch information
Showing
8 changed files
with
285 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...ramework/common/src/test/java/io/quarkus/test/common/TestResourceManagerInjectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.quarkus.test.common; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestResourceManagerInjectorTest { | ||
|
||
@Test | ||
void testTestInjector() { | ||
TestResourceManager manager = new TestResourceManager(UsingInjectorTest.class); | ||
manager.start(); | ||
|
||
Foo foo = new Foo(); | ||
manager.inject(foo); | ||
|
||
Assertions.assertNotNull(foo.bar); | ||
Assertions.assertEquals("bar", foo.bar.value); | ||
Assertions.assertNotNull(foo.dummy); | ||
Assertions.assertEquals("dummy", foo.dummy.value); | ||
} | ||
|
||
@QuarkusTestResource(UsingTestInjectorLifecycleManager.class) | ||
public static class UsingInjectorTest { | ||
} | ||
|
||
public static class UsingTestInjectorLifecycleManager implements QuarkusTestResourceLifecycleManager { | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
|
||
} | ||
|
||
@Override | ||
public void inject(TestInjector testInjector) { | ||
TestResourceManager.DefaultTestInjector defaultTestInjector = (TestResourceManager.DefaultTestInjector) testInjector; | ||
if (defaultTestInjector.testInstance instanceof Foo) { | ||
testInjector.injectIntoFields(new Bar("bar"), (f) -> f.getType().isAssignableFrom(BarBase.class)); | ||
testInjector.injectIntoFields(new Dummy("dummy"), (f) -> f.getType().equals(Dummy.class)); | ||
} | ||
} | ||
} | ||
|
||
public static abstract class BarBase { | ||
public final String value; | ||
|
||
public BarBase(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
public static class Bar extends BarBase { | ||
public Bar(String value) { | ||
super(value); | ||
} | ||
} | ||
|
||
public static class Dummy { | ||
public final String value; | ||
|
||
public Dummy(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
public static class Foo { | ||
BarBase bar; | ||
@InjectDummy | ||
Dummy dummy; | ||
} | ||
|
||
@Target({ FIELD }) | ||
@Retention(RUNTIME) | ||
public @interface InjectDummy { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.