Skip to content

Commit 1470498

Browse files
authored
[test] add a switch to create a scope per test (avaje#859)
1 parent 90cc092 commit 1470498

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

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

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

3-
import org.junit.jupiter.api.extension.ExtendWith;
4-
53
import java.lang.annotation.ElementType;
64
import java.lang.annotation.Retention;
75
import java.lang.annotation.RetentionPolicy;
86
import java.lang.annotation.Target;
97

8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
1010
/**
1111
* An avaje-inject test supporting {@code @Inject} along with Mockito annotations -
1212
* {@code @Mock, @Spy, @Captor}.
@@ -20,4 +20,7 @@
2020

2121
/** Wiring profiles to use */
2222
String[] profiles() default {};
23+
24+
/** Create a new test beanscope for each test methods */
25+
boolean scopePerMethod() default false;
2326
}

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

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

3+
import java.util.Optional;
4+
35
import io.avaje.inject.BeanScope;
46
import io.avaje.inject.BeanScopeBuilder;
57

6-
import java.util.Optional;
7-
88
/**
99
* Wraps the underlying metadata (fields with annotations @Mock, @Spy, @Inject, @Captor).
1010
*/
@@ -45,14 +45,17 @@ private TestBeans buildSet(GlobalTestBeans.Beans parent, Object testInstance) {
4545
}
4646

4747
private TestBeans buildTestBeans(GlobalTestBeans.Beans parent, Object testInstance) {
48+
var injectTest =
49+
Optional.ofNullable(testInstance)
50+
.map(Object::getClass)
51+
.map(c -> c.getAnnotation(InjectTest.class));
52+
4853
// wiring profiles
49-
String[] profiles = Optional.ofNullable(testInstance)
50-
.map(Object::getClass)
51-
.map(c -> c.getAnnotation(InjectTest.class))
52-
.map(InjectTest::profiles)
53-
.orElse(new String[0]);
54+
String[] profiles = injectTest.map(InjectTest::profiles).orElse(new String[0]);
5455

55-
if (profiles.length > 0 || reader.hasMocksOrSpies(testInstance)) {
56+
if (profiles.length > 0
57+
|| injectTest.map(InjectTest::scopePerMethod).orElse(false)
58+
|| reader.hasMocksOrSpies(testInstance)) {
5659
// need to build a BeanScope for this using baseBeans() as the parent
5760
final BeanScopeBuilder builder = BeanScope.builder();
5861
if (parent != null) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.example.injectextension;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import io.avaje.inject.test.InjectTest;
8+
import jakarta.inject.Inject;
9+
10+
@InjectTest(scopePerMethod = true)
11+
class ScopePerMethodTest {
12+
@Inject ƎNA ena;
13+
static ƎNA ayna;
14+
15+
@Test
16+
void one() {
17+
assertThat(ena).isNotSameAs(ayna);
18+
ayna = ena;
19+
}
20+
21+
@Test
22+
void two() {
23+
assertThat(ena).isNotSameAs(ayna);
24+
ayna = ena;
25+
}
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example.injectextension;
2+
3+
import jakarta.inject.Singleton;
4+
5+
@Singleton
6+
public class ƎNA {
7+
8+
public String greet() {
9+
return "Ah, these are the most utmost grand of days";
10+
}
11+
12+
public void giveGift() {}
13+
}

0 commit comments

Comments
 (0)