Skip to content

[test] Add a switch to create a scope per test #859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.avaje.inject.test;

import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* An avaje-inject test supporting {@code @Inject} along with Mockito annotations -
* {@code @Mock, @Spy, @Captor}.
Expand All @@ -20,4 +20,7 @@

/** Wiring profiles to use */
String[] profiles() default {};

/** Create a new test beanscope for each test methods */
boolean scopePerMethod() default false;
}
19 changes: 11 additions & 8 deletions inject-test/src/main/java/io/avaje/inject/test/MetaInfo.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.avaje.inject.test;

import java.util.Optional;

import io.avaje.inject.BeanScope;
import io.avaje.inject.BeanScopeBuilder;

import java.util.Optional;

/**
* Wraps the underlying metadata (fields with annotations @Mock, @Spy, @Inject, @Captor).
*/
Expand Down Expand Up @@ -45,14 +45,17 @@ private TestBeans buildSet(GlobalTestBeans.Beans parent, Object testInstance) {
}

private TestBeans buildTestBeans(GlobalTestBeans.Beans parent, Object testInstance) {
var injectTest =
Optional.ofNullable(testInstance)
.map(Object::getClass)
.map(c -> c.getAnnotation(InjectTest.class));

// wiring profiles
String[] profiles = Optional.ofNullable(testInstance)
.map(Object::getClass)
.map(c -> c.getAnnotation(InjectTest.class))
.map(InjectTest::profiles)
.orElse(new String[0]);
String[] profiles = injectTest.map(InjectTest::profiles).orElse(new String[0]);

if (profiles.length > 0 || reader.hasMocksOrSpies(testInstance)) {
if (profiles.length > 0
|| injectTest.map(InjectTest::scopePerMethod).orElse(false)
|| reader.hasMocksOrSpies(testInstance)) {
// need to build a BeanScope for this using baseBeans() as the parent
final BeanScopeBuilder builder = BeanScope.builder();
if (parent != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.example.injectextension;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import io.avaje.inject.test.InjectTest;
import jakarta.inject.Inject;

@InjectTest(scopePerMethod = true)
class ScopePerMethodTest {
@Inject ƎNA ena;
static ƎNA ayna;

@Test
void one() {
assertThat(ena).isNotSameAs(ayna);
ayna = ena;
}

@Test
void two() {
assertThat(ena).isNotSameAs(ayna);
ayna = ena;
}
}
13 changes: 13 additions & 0 deletions inject-test/src/test/java/org/example/injectextension/ƎNA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.example.injectextension;

import jakarta.inject.Singleton;

@Singleton
public class ƎNA {

public String greet() {
return "Ah, these are the most utmost grand of days";
}

public void giveGift() {}
}