Skip to content

Commit 45cda7d

Browse files
committed
fix test generic injection
1 parent 0bcc051 commit 45cda7d

File tree

11 files changed

+512
-45
lines changed

11 files changed

+512
-45
lines changed

blackbox-test-inject/src/main/java/org/example/myapp/generic/GenericFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class GenericFactory {
1010

1111
@Bean
12-
Generic<String> one() {
12+
Generic<Integer> one() {
1313

1414
return new Generic<>() {};
1515
}

inject-test/src/main/java/io/avaje/inject/test/MetaReader.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.lang.annotation.Annotation;
1616
import java.lang.reflect.Field;
1717
import java.lang.reflect.Modifier;
18+
import java.lang.reflect.Type;
1819
import java.util.ArrayList;
1920
import java.util.List;
2021

@@ -53,14 +54,14 @@ boolean hasInstanceInjection() {
5354

5455
@Override
5556
public String toString() {
56-
String s = toStringAppend("mocks:", mocks);
57-
s += toStringAppend("spies:", spies);
58-
s += toStringAppend("inject:", injection);
59-
s += toStringAppend("captors:", captors);
60-
s += toStringAppend("staticMocks:", staticMocks);
61-
s += toStringAppend("staticSpies:", staticSpies);
62-
s += toStringAppend("staticInjection:", staticInjection);
63-
return s;
57+
StringBuilder s = new StringBuilder().append(toStringAppend("mocks:", mocks));
58+
s.append(toStringAppend("spies:", spies));
59+
s.append(toStringAppend("inject:", injection));
60+
s.append(toStringAppend("captors:", captors));
61+
s.append(toStringAppend("staticMocks:", staticMocks));
62+
s.append(toStringAppend("staticSpies:", staticSpies));
63+
s.append(toStringAppend("staticInjection:", staticInjection));
64+
return s.toString();
6465
}
6566

6667
private String toStringAppend(String key, List<?> entries) {
@@ -282,8 +283,8 @@ public String toString() {
282283
return field.getName();
283284
}
284285

285-
Class<?> type() {
286-
return field.getType();
286+
Type type() {
287+
return field.getGenericType();
287288
}
288289

289290
String name() {

inject-test/src/main/java/io/avaje/inject/test/Plugin.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.inject.test;
22

3+
import java.lang.reflect.Type;
4+
35
import io.avaje.inject.BeanScope;
46

57
/**
@@ -13,7 +15,7 @@ public interface Plugin {
1315
/**
1416
* Return true if this plugin should inject for this class.
1517
*/
16-
boolean forType(Class<?> type);
18+
boolean forType(Type type);
1719

1820
/**
1921
* Create a plugin scope for either EACH or ALL (per test or per class)
@@ -32,7 +34,7 @@ interface Scope {
3234
* <p>
3335
* For example, avaje-jex-test will create a http client.
3436
*/
35-
Object create(Class<?> type);
37+
Object create(Type type);
3638

3739
/**
3840
* Close resources that are in this scope - for example, http server.

inject-test/src/test/java/io/avaje/inject/test/MetaReaderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.mockito.Mock;
1010
import org.mockito.Mockito;
1111

12+
import java.lang.reflect.Type;
1213
import java.net.http.HttpClient;
1314

1415
import static org.assertj.core.api.Assertions.assertThat;
@@ -49,7 +50,7 @@ static class HelloBean {
4950

5051
static class MyPlugin implements Plugin {
5152

52-
public boolean forType(Class<?> type) {
53+
public boolean forType(Type type) {
5354
return HttpClient.class.equals(type);
5455
}
5556

@@ -63,7 +64,7 @@ static class Scope implements Plugin.Scope {
6364
HttpClient httpClient;
6465

6566
@Override
66-
public Object create(Class<?> type) {
67+
public Object create(Type type) {
6768
this.httpClient = HttpClient.newBuilder().build();
6869
return httpClient;
6970
}

inject/src/main/java/io/avaje/inject/BeanScope.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.avaje.inject;
22

3+
import io.avaje.inject.spi.Types;
34
import io.avaje.lang.NonNullApi;
45
import io.avaje.lang.Nullable;
56

@@ -140,7 +141,51 @@ static BeanScopeBuilder builder() {
140141
<T> T get(Class<T> type, @Nullable String name);
141142

142143
/**
143-
* Return a single bean given the generic type and name.
144+
* Return a single bean given its type and generic components.
145+
*
146+
* <pre>{@code
147+
* Map<String, CoffeeMaker> coffeeMaker = beanScope.get(Map.class, String.class, CoffeeMaker.class);
148+
*
149+
* }</pre>
150+
*
151+
* @param base the base generic type
152+
* @param type component types of the generic type
153+
* @throws java.util.NoSuchElementException When no matching bean is found
154+
*/
155+
default <T> T get(Type base, Type... types) {
156+
return get(null, base, types);
157+
}
158+
159+
/**
160+
* Return a single bean given it's name, type, and generic components.
161+
*
162+
* <pre>{@code
163+
* Heater heater = beanScope.get("electric", Set.class, );
164+
* heater.heat();
165+
*
166+
* }</pre>
167+
*
168+
* @param type an interface or bean type
169+
* @param name the name qualifier of a specific bean
170+
* @throws java.util.NoSuchElementException When no matching bean is found
171+
*/
172+
default <T> T get(@Nullable String name, Type type, Type... types) {
173+
174+
return get(Types.parameterizedType(type, types), name);
175+
}
176+
177+
/**
178+
* Return a single bean given the full generic type.
179+
*
180+
* @param type The generic type
181+
* @throws java.util.NoSuchElementException When no matching bean is found
182+
*/
183+
default <T> T get(Type type) {
184+
return get(type, (String) null);
185+
}
186+
187+
/**
188+
* Return a single bean given the full generic type and name.
144189
*
145190
* @param type The generic type
146191
* @param name the name qualifier of a specific bean

inject/src/main/java/io/avaje/inject/BeanScopeBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ interface ForTesting extends BeanScopeBuilder {
325325
*
326326
* }</pre>
327327
*/
328-
BeanScopeBuilder.ForTesting mock(Class<?> type);
328+
BeanScopeBuilder.ForTesting mock(Type type);
329329

330330
/**
331331
* Register as a Mockito mock with a qualifier name.
@@ -343,7 +343,7 @@ interface ForTesting extends BeanScopeBuilder {
343343
*
344344
* }</pre>
345345
*/
346-
BeanScopeBuilder.ForTesting mock(Class<?> type, String name);
346+
BeanScopeBuilder.ForTesting mock(Type type, String name);
347347

348348
/**
349349
* Use a mockito mock when injecting this bean type additionally
@@ -398,7 +398,7 @@ interface ForTesting extends BeanScopeBuilder {
398398
*
399399
* }</pre>
400400
*/
401-
BeanScopeBuilder.ForTesting spy(Class<?> type);
401+
BeanScopeBuilder.ForTesting spy(Type type);
402402

403403
/**
404404
* Register a Mockito spy with a qualifier name.
@@ -416,7 +416,7 @@ interface ForTesting extends BeanScopeBuilder {
416416
*
417417
* }</pre>
418418
*/
419-
BeanScopeBuilder.ForTesting spy(Class<?> type, String name);
419+
BeanScopeBuilder.ForTesting spy(Type type, String name);
420420

421421
/**
422422
* Use a mockito spy when injecting this bean type additionally

inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,14 @@ public BeanScopeBuilder parent(BeanScope parent, boolean parentOverride) {
157157
}
158158

159159
@Override
160-
public BeanScopeBuilder.ForTesting mock(Class<?> type) {
161-
return mock(type, null, null);
160+
public BeanScopeBuilder.ForTesting mock(Type type) {
161+
return mock(type, null);
162162
}
163163

164164
@Override
165-
public BeanScopeBuilder.ForTesting mock(Class<?> type, String name) {
166-
return mock(type, name, null);
165+
public BeanScopeBuilder.ForTesting mock(Type type, String name) {
166+
suppliedBeans.add(SuppliedBean.ofType(name, type, null));
167+
return this;
167168
}
168169

169170
@Override
@@ -177,12 +178,12 @@ private <D> BeanScopeBuilder.ForTesting mock(Class<D> type, @Nullable String nam
177178
}
178179

179180
@Override
180-
public BeanScopeBuilder.ForTesting spy(Class<?> type) {
181+
public BeanScopeBuilder.ForTesting spy(Type type) {
181182
return spy(type, null, null);
182183
}
183184

184185
@Override
185-
public BeanScopeBuilder.ForTesting spy(Class<?> type, String name) {
186+
public BeanScopeBuilder.ForTesting spy(Type type, String name) {
186187
return spy(type, name, null);
187188
}
188189

@@ -196,6 +197,12 @@ private <D> BeanScopeBuilder.ForTesting spy(Class<D> type, @Nullable String name
196197
return this;
197198
}
198199

200+
private <D> BeanScopeBuilder.ForTesting spy(
201+
Type type, @Nullable String name, @Nullable Consumer<D> consumer) {
202+
enrichBeans.add(new EnrichBean<>(type, name, consumer));
203+
return this;
204+
}
205+
199206
private void initClassLoader() {
200207
if (classLoader == null) {
201208
classLoader = Thread.currentThread().getContextClassLoader();

inject/src/main/java/io/avaje/inject/spi/EnrichBean.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
import org.mockito.Mockito;
44

5+
import java.lang.reflect.Type;
56
import java.util.function.Consumer;
67

78
/**
89
* Holds Spy setup consumers for dependency injection using Mockito Spy.
910
*/
1011
public final class EnrichBean<B> {
1112

12-
private final Class<B> type;
13+
private final Type type;
1314
private final String name;
1415
private final Consumer<B> consumer;
1516

16-
public EnrichBean(Class<B> type, String name, Consumer<B> consumer) {
17+
public EnrichBean(Type type, String name, Consumer<B> consumer) {
1718
this.type = type;
1819
this.name = KeyUtil.lower(name);
1920
this.consumer = consumer;

0 commit comments

Comments
 (0)