From bcfdebe16aced896c1fbfc8e112c3ffb3182bc46 Mon Sep 17 00:00:00 2001 From: Ivan Nikitka <70625960+Ivan-nikitko@users.noreply.github.com> Date: Mon, 9 Jan 2023 14:45:43 +0100 Subject: [PATCH] added test for injection with annotation "@Named" --- .../bootique/jersey/ResourceInjectionIT.java | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/bootique-jersey/src/test/java/io/bootique/jersey/ResourceInjectionIT.java b/bootique-jersey/src/test/java/io/bootique/jersey/ResourceInjectionIT.java index 9f7936f..c2ce2be 100644 --- a/bootique-jersey/src/test/java/io/bootique/jersey/ResourceInjectionIT.java +++ b/bootique-jersey/src/test/java/io/bootique/jersey/ResourceInjectionIT.java @@ -21,6 +21,7 @@ import io.bootique.BQRuntime; import io.bootique.Bootique; +import io.bootique.di.BQInject; import io.bootique.jetty.junit5.JettyTester; import io.bootique.junit5.BQApp; import io.bootique.junit5.BQTest; @@ -46,6 +47,8 @@ public class ResourceInjectionIT { private static final String TEST_PROPERTY = "bq.test.label"; private static final InjectedService service = new InjectedService(); + private static final InjectedServiceInterface serviceA = new InjectedServiceImplA(); + private static final InjectedServiceInterface serviceB = new InjectedServiceImplB(); static final JettyTester jetty = JettyTester.create(); @@ -53,7 +56,8 @@ public class ResourceInjectionIT { static final BQRuntime app = Bootique.app("-s") .autoLoadModules() .module(b -> b.bind(InjectedService.class).toInstance(service)) - .module(b -> b.bind(InjectedService.class, "namedService").toInstance(service)) + .module(b -> b.bind(InjectedServiceInterface.class, "A").toInstance(serviceA)) + .module(b -> b.bind(InjectedServiceInterface.class, "B").toInstance(serviceB)) .module(b -> b.bind(UnInjectedResource.class).toProviderInstance(() -> new UnInjectedResource(service))) .module(b -> JerseyModule.extend(b) .addFeature(ctx -> { @@ -91,12 +95,12 @@ public void testNamedFieldInjected() { Response r1 = jetty.getTarget().path("nf").request().get(); assertEquals(Status.OK.getStatusCode(), r1.getStatus()); - assertEquals("nf_1_x", r1.readEntity(String.class)); + assertEquals("nf_1_2_x", r1.readEntity(String.class)); r1.close(); Response r2 = jetty.getTarget().path("nf").request().get(); assertEquals(Status.OK.getStatusCode(), r2.getStatus()); - assertEquals("nf_2_x", r2.readEntity(String.class)); + assertEquals("nf_3_4_x", r2.readEntity(String.class)); r2.close(); } @@ -149,15 +153,19 @@ public String get() { public static class NamedFieldInjectedResource { @Inject - @Named("namedService") - private InjectedService service; + @Named("A") + private InjectedServiceInterface serviceA; + + @Inject + @Named("B") + private InjectedServiceInterface serviceB; @Context private Configuration config; @GET public String get() { - return "nf_" + service.getNext() + "_" + config.getProperty(TEST_PROPERTY); + return "nf_" + serviceA.getNext() + "_" + serviceA.getNext()+ "_" + config.getProperty(TEST_PROPERTY); } } @@ -212,4 +220,37 @@ public int getNext() { return atomicInt.incrementAndGet(); } } + + public static interface InjectedServiceInterface { + + AtomicInteger atomicInt = new AtomicInteger(); + public void reset(); + public int getNext(); + } + + public static class InjectedServiceImplA implements InjectedServiceInterface { + + private AtomicInteger atomicInt = new AtomicInteger(); + + public void reset() { + atomicInt.set(0); + } + + public int getNext() { + return atomicInt.incrementAndGet(); + } + } + + public static class InjectedServiceImplB implements InjectedServiceInterface { + + private AtomicInteger atomicInt = new AtomicInteger(); + + public void reset() { + atomicInt.set(0); + } + + public int getNext() { + return atomicInt.incrementAndGet(); + } + } }