Skip to content

#200 - ENH: Modify InjectExtension to support multiple TestModule #201

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

Merged
merged 1 commit into from
May 1, 2022
Merged
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
25 changes: 14 additions & 11 deletions inject-test/src/main/java/io/avaje/inject/test/InjectExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.avaje.inject.BeanScope;
import io.avaje.inject.BeanScopeBuilder;
import io.avaje.inject.spi.Module;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
Expand All @@ -11,12 +12,11 @@
import java.io.*;
import java.lang.System.Logger.Level;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

import static java.util.Collections.singletonList;

/**
* Junit 5 extension for avaje inject.
* <p>
Expand Down Expand Up @@ -58,18 +58,21 @@ public void close() {
}

private void initialiseGlobalTestScope(ExtensionContext context) {
Iterator<TestModule> iterator = ServiceLoader.load(TestModule.class).iterator();
if (iterator.hasNext()) {
registerTestModule(context, iterator.next());
} else {
List<TestModule> testModules = new ArrayList<>();
for (TestModule next : ServiceLoader.load(TestModule.class)) {
testModules.add(next);
}
if (testModules.isEmpty()) {
registerViaResources(context);
} else {
registerTestModule(context, testModules);
}
}

private void registerTestModule(ExtensionContext context, TestModule testModule) {
private void registerTestModule(ExtensionContext context, List<TestModule> testModules) {
log.log(Level.DEBUG, "Building global test BeanScope (as parent scope for tests)");
globalTestScope = BeanScope.newBuilder()
.withModules(testModule)
.withModules(testModules.toArray(Module[]::new))
.build();

log.log(Level.TRACE, "register global test BeanScope with beans %s", globalTestScope);
Expand All @@ -86,7 +89,7 @@ private void registerViaResources(ExtensionContext context) {
if (className != null) {
Class<?> cls = Class.forName(className);
TestModule testModule = (TestModule) cls.getDeclaredConstructor().newInstance();
registerTestModule(context, testModule);
registerTestModule(context, singletonList(testModule));
}
} catch (Throwable e) {
throw new RuntimeException("Error trying to create TestModule", e);
Expand Down