Skip to content

Commit

Permalink
junit parameter resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-qnz authored and manovotn committed Jan 5, 2024
1 parent 8083f51 commit 3b7c5a6
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 16 deletions.
2 changes: 1 addition & 1 deletion junit5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ This section describes any additional configuration options this extension offer
### Explicit Parameter Injection

As mentioned above, Weld is greedy when it comes to parameter injection.
It will claim the ability to resolve any parameter which is known as a bean type inside the running CDI container.
It will claim the ability to resolve any parameter which is known as a bean type inside the running CDI container except the ones built into JUnit itself.
This is mainly for usability, as it would be annoying to constantly type additional annotations to mark which parameter should be injected and which should be left alone.

However, we are aware that this might cause trouble if more extensions are competing for parameter resolution.
Expand Down
55 changes: 40 additions & 15 deletions junit5/src/main/java/org/jboss/weld/junit5/WeldJunit5Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.inject.WeldInstance;
import org.jboss.weld.util.collections.ImmutableList;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
Expand All @@ -54,6 +57,7 @@
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.io.TempDir;

/**
* JUnit 5 extension allowing to bootstrap Weld SE container for each @Test method (or once per test class
Expand Down Expand Up @@ -108,16 +112,6 @@ private static void storeExplicitParamResolutionInformation(ExtensionContext ec)

}

@Override
public void afterAll(ExtensionContext context) {
if (determineTestLifecycle(context).equals(PER_CLASS)) {
WeldInitiator initiator = getInitiatorFromStore(context);
if (initiator != null) {
initiator.shutdownWeld();
}
}
}

@Override
public void beforeAll(ExtensionContext context) {
// we are storing them into root context, hence only needs to be done once per test suite
Expand All @@ -130,6 +124,11 @@ public void beforeAll(ExtensionContext context) {
startWeldContainerIfAppropriate(PER_CLASS, context);
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
startWeldContainerIfAppropriate(PER_METHOD, extensionContext);
}

@Override
public void afterEach(ExtensionContext context) {
if (determineTestLifecycle(context).equals(PER_METHOD)) {
Expand All @@ -140,6 +139,16 @@ public void afterEach(ExtensionContext context) {
}
}

@Override
public void afterAll(ExtensionContext context) {
if (determineTestLifecycle(context).equals(PER_CLASS)) {
WeldInitiator initiator = getInitiatorFromStore(context);
if (initiator != null) {
initiator.shutdownWeld();
}
}
}

protected void weldInit(ExtensionContext context, Weld weld, WeldInitiator.Builder weldInitiatorBuilder) {
weld.addPackage(false, context.getRequiredTestClass());
}
Expand All @@ -162,6 +171,10 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
// do not attempt to resolve JUnit 5 built-in parameters
if (isJUnitResolvedParameter(parameterContext)) {
return false;
}
// if weld container isn't up yet or if it's not Method, we don't resolve it
if (getContainerFromStore(extensionContext) == null
|| (!(parameterContext.getDeclaringExecutable() instanceof Method))) {
Expand Down Expand Up @@ -191,6 +204,23 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
}
}

/**
* @see {@code org.junit.jupiter.engine.extension.TestInfoParameterResolver.supportsParameter}
* @see {@code org.junit.jupiter.engine.extension.RepetitionExtension.supportsParameter}
* @see {@code org.junit.jupiter.engine.extension.TestReporterParameterResolver.supportsParameter}
* @see {@code org.junit.jupiter.engine.extension.TempDirectory.supportsParameter}
*/
private boolean isJUnitResolvedParameter(ParameterContext parameterContext) {
Class<?> type = parameterContext.getParameter().getType();
if (type == TestInfo.class || type == RepetitionInfo.class || type == TestReporter.class) {
return true;
}
if (parameterContext.isAnnotated(TempDir.class)) {
return true;
}
return false;
}

private List<Annotation> resolveQualifiers(ParameterContext pc, BeanManager bm) {
List<Annotation> qualifiers = new ArrayList<>();
if (pc.getParameter().getAnnotations().length == 0) {
Expand Down Expand Up @@ -225,11 +255,6 @@ private TestInstance.Lifecycle determineTestLifecycle(ExtensionContext ec) {
}
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
startWeldContainerIfAppropriate(PER_METHOD, extensionContext);
}

private void startWeldContainerIfAppropriate(TestInstance.Lifecycle expectedLifecycle, ExtensionContext context) {
// if the lifecycle is what we expect it to be, start Weld container
if (determineTestLifecycle(context).equals(expectedLifecycle)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jboss.weld.junit5.compat;

import org.jboss.weld.junit5.auto.EnableAutoWeld;

@EnableAutoWeld
class JunitParameterResolverAutoWeldTest extends JunitParameterResolverReferenceTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jboss.weld.junit5.compat;

import java.nio.file.Path;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.api.io.TempDir;

/**
* This test runs without CDI in order to demonstrate that it works with plain JUnit.
*/
class JunitParameterResolverReferenceTest {

@Test
void testResolveTestInfo(TestInfo testInfo) {
Assertions.assertNotNull(testInfo);
}

@RepeatedTest(1)
void testResolveRepetitionInfo(RepetitionInfo repetitionInfo) {
Assertions.assertNotNull(repetitionInfo);
}

@Test
void testResolveTestReporter(TestReporter testReporter) {
Assertions.assertNotNull(testReporter);
}

@Test
void testResolveTempDir(@TempDir Path tempDir) {
Assertions.assertNotNull(tempDir);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jboss.weld.junit5.compat;

import org.jboss.weld.junit5.EnableWeld;

@EnableWeld
class JunitParameterResolverWeldTest extends JunitParameterResolverReferenceTest {

}

0 comments on commit 3b7c5a6

Please sign in to comment.