|
1 | 1 | package org.jooby.issues; |
2 | 2 |
|
3 | | -import com.google.inject.Guice; |
4 | | -import com.google.inject.Injector; |
| 3 | +import java.util.concurrent.atomic.AtomicReference; |
| 4 | + |
5 | 5 | import org.jooby.test.ServerFeature; |
6 | | -import org.junit.*; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
7 | 8 |
|
8 | | -import java.util.concurrent.atomic.AtomicReference; |
| 9 | +import com.google.inject.Guice; |
| 10 | +import com.google.inject.Injector; |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * @author Johannes Schneider (<a href="mailto:[email protected]">[email protected]</a>) |
12 | 14 | */ |
13 | 15 | public class PullRequest583WithChildInjectorTest extends ServerFeature { |
14 | | - private final AtomicReference<Injector> createdInjector = new AtomicReference<>(); |
15 | | - |
16 | | - private final Injector parentInjector = Guice.createInjector( |
17 | | - binder -> binder.bind(MyInjectedClass.class).toInstance(new MyInjectedClass())); |
18 | 16 |
|
19 | 17 | { |
| 18 | + AtomicReference<Injector> ref = new AtomicReference<>(); |
| 19 | + |
| 20 | + Injector parentInjector = Guice.createInjector( |
| 21 | + binder -> binder.bind(MyInjectedClass.class).toInstance(new MyInjectedClass())); |
20 | 22 |
|
21 | 23 | injector((stage, module) -> { |
22 | 24 | Injector injector = parentInjector.createChildInjector(module); |
23 | | - createdInjector.set(injector); |
| 25 | + ref.set(injector); |
24 | 26 | return injector; |
25 | 27 | }); |
| 28 | + |
| 29 | + get("/583", () -> { |
| 30 | + Injector injector = require(Injector.class); |
| 31 | + Assert.assertSame(injector, ref.get()); |
| 32 | + Assert.assertSame(parentInjector.getInstance(MyInjectedClass.class), |
| 33 | + injector.getInstance(MyInjectedClass.class)); |
| 34 | + return "OK"; |
| 35 | + }); |
26 | 36 | } |
27 | 37 |
|
28 | 38 | @Test |
29 | | - public void appShouldBeMountedOnApplicationPath() throws Exception { |
30 | | - Assert.assertNull(createdInjector.get()); |
31 | | - |
32 | | - start(); |
33 | | - try { |
34 | | - Injector injector = require(Injector.class); |
35 | | - Assert.assertSame(parentInjector.getInstance(MyInjectedClass.class), injector.getInstance(MyInjectedClass.class)); |
36 | | - } finally { |
37 | | - stop(); |
38 | | - } |
| 39 | + public void childInjector() throws Exception { |
| 40 | + request() |
| 41 | + .get("/583") |
| 42 | + .expect("OK"); |
39 | 43 | } |
40 | 44 |
|
41 | 45 | private static class MyInjectedClass { |
|
0 commit comments