Skip to content

Commit

Permalink
Merge pull request #46474 from masecla22/main
Browse files Browse the repository at this point in the history
Ensure that QuarkusUnitTest plays nicely with `@TestFactory`
  • Loading branch information
geoand authored Feb 26, 2025
2 parents 453add3 + 1806991 commit 1ba5ca1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkus.arc.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.List;

import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class SimpleBeanTestFactoryTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(SimpleBean.class)
.addAsResource(new StringAsset("simpleBean.baz=1"), "application.properties"));

@Inject
SimpleBean simpleBean;

@TestFactory
public List<DynamicTest> testBeanInsideFactory() {
return List.of(
DynamicTest.dynamicTest("test 1", () -> {
assertNotNull(simpleBean);
}),
DynamicTest.dynamicTest("test 2", () -> {
assertEquals(1, 1);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ public void interceptBeforeEachMethod(Invocation<Void> invocation, ReflectiveInv
invocation.skip();
}

@Override
public <T> T interceptTestFactoryMethod(Invocation<T> invocation,
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
T result = (T) runExtensionMethod(invocationContext, extensionContext, false);
invocation.skip();
return result;
}

@Override
public void interceptAfterEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
Expand Down Expand Up @@ -438,7 +446,7 @@ public void interceptTestTemplateMethod(Invocation<Void> invocation, ReflectiveI
invocation.skip();
}

private void runExtensionMethod(ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext,
private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext,
boolean testMethodInvokersAllowed) throws Throwable {
Method newMethod = null;
Class<?> c = actualTestClass;
Expand Down Expand Up @@ -505,12 +513,12 @@ private void runExtensionMethod(ReflectiveInvocationContext<Method> invocationCo
effectiveArguments.add(originalValue);
}
}
testMethodInvokerToUse.getClass()
return testMethodInvokerToUse.getClass()
.getMethod("invoke", Object.class, Method.class, List.class, String.class)
.invoke(testMethodInvokerToUse, actualTestInstance, newMethod, effectiveArguments,
extensionContext.getRequiredTestClass().getName());
} else {
newMethod.invoke(actualTestInstance, invocationContext.getArguments().toArray());
return newMethod.invoke(actualTestInstance, invocationContext.getArguments().toArray());
}
} catch (InvocationTargetException e) {
throw e.getCause();
Expand Down

0 comments on commit 1ba5ca1

Please sign in to comment.