Skip to content

Commit bcfdebe

Browse files
committed
added test for injection with annotation "@nAmed"
1 parent 369a520 commit bcfdebe

1 file changed

Lines changed: 47 additions & 6 deletions

File tree

bootique-jersey/src/test/java/io/bootique/jersey/ResourceInjectionIT.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import io.bootique.BQRuntime;
2323
import io.bootique.Bootique;
24+
import io.bootique.di.BQInject;
2425
import io.bootique.jetty.junit5.JettyTester;
2526
import io.bootique.junit5.BQApp;
2627
import io.bootique.junit5.BQTest;
@@ -46,14 +47,17 @@ public class ResourceInjectionIT {
4647

4748
private static final String TEST_PROPERTY = "bq.test.label";
4849
private static final InjectedService service = new InjectedService();
50+
private static final InjectedServiceInterface serviceA = new InjectedServiceImplA();
51+
private static final InjectedServiceInterface serviceB = new InjectedServiceImplB();
4952

5053
static final JettyTester jetty = JettyTester.create();
5154

5255
@BQApp
5356
static final BQRuntime app = Bootique.app("-s")
5457
.autoLoadModules()
5558
.module(b -> b.bind(InjectedService.class).toInstance(service))
56-
.module(b -> b.bind(InjectedService.class, "namedService").toInstance(service))
59+
.module(b -> b.bind(InjectedServiceInterface.class, "A").toInstance(serviceA))
60+
.module(b -> b.bind(InjectedServiceInterface.class, "B").toInstance(serviceB))
5761
.module(b -> b.bind(UnInjectedResource.class).toProviderInstance(() -> new UnInjectedResource(service)))
5862
.module(b -> JerseyModule.extend(b)
5963
.addFeature(ctx -> {
@@ -91,12 +95,12 @@ public void testNamedFieldInjected() {
9195

9296
Response r1 = jetty.getTarget().path("nf").request().get();
9397
assertEquals(Status.OK.getStatusCode(), r1.getStatus());
94-
assertEquals("nf_1_x", r1.readEntity(String.class));
98+
assertEquals("nf_1_2_x", r1.readEntity(String.class));
9599
r1.close();
96100

97101
Response r2 = jetty.getTarget().path("nf").request().get();
98102
assertEquals(Status.OK.getStatusCode(), r2.getStatus());
99-
assertEquals("nf_2_x", r2.readEntity(String.class));
103+
assertEquals("nf_3_4_x", r2.readEntity(String.class));
100104
r2.close();
101105
}
102106

@@ -149,15 +153,19 @@ public String get() {
149153
public static class NamedFieldInjectedResource {
150154

151155
@Inject
152-
@Named("namedService")
153-
private InjectedService service;
156+
@Named("A")
157+
private InjectedServiceInterface serviceA;
158+
159+
@Inject
160+
@Named("B")
161+
private InjectedServiceInterface serviceB;
154162

155163
@Context
156164
private Configuration config;
157165

158166
@GET
159167
public String get() {
160-
return "nf_" + service.getNext() + "_" + config.getProperty(TEST_PROPERTY);
168+
return "nf_" + serviceA.getNext() + "_" + serviceA.getNext()+ "_" + config.getProperty(TEST_PROPERTY);
161169
}
162170
}
163171

@@ -212,4 +220,37 @@ public int getNext() {
212220
return atomicInt.incrementAndGet();
213221
}
214222
}
223+
224+
public static interface InjectedServiceInterface {
225+
226+
AtomicInteger atomicInt = new AtomicInteger();
227+
public void reset();
228+
public int getNext();
229+
}
230+
231+
public static class InjectedServiceImplA implements InjectedServiceInterface {
232+
233+
private AtomicInteger atomicInt = new AtomicInteger();
234+
235+
public void reset() {
236+
atomicInt.set(0);
237+
}
238+
239+
public int getNext() {
240+
return atomicInt.incrementAndGet();
241+
}
242+
}
243+
244+
public static class InjectedServiceImplB implements InjectedServiceInterface {
245+
246+
private AtomicInteger atomicInt = new AtomicInteger();
247+
248+
public void reset() {
249+
atomicInt.set(0);
250+
}
251+
252+
public int getNext() {
253+
return atomicInt.incrementAndGet();
254+
}
255+
}
215256
}

0 commit comments

Comments
 (0)