Skip to content

Commit

Permalink
added test for injection with annotation "@nAmed"
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-nikitko committed Jan 9, 2023
1 parent 369a520 commit bcfdebe
Showing 1 changed file with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,14 +47,17 @@ 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();

@BQApp
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 -> {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit bcfdebe

Please sign in to comment.