-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly support JUnit 5 parameters on test methods
This is done by deep-cloning the objects into the TCCL before actually passing them to the test Fixes #8251, fixes #8703, fixes #8978
- Loading branch information
Showing
7 changed files
with
192 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
integration-tests/main/src/test/java/io/quarkus/it/main/MethodSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.quarkus.it.main; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.hamcrest.Matcher; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import io.quarkus.it.arc.UnusedBean; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class MethodSourceTest { | ||
|
||
@Inject | ||
UnusedBean unusedBean; | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideDummyInput") | ||
public void testParameterResolver(UnusedBean.DummyInput dummyInput, Matcher<String> matcher) { | ||
UnusedBean.DummyResult dummyResult = unusedBean.dummy(dummyInput); | ||
assertThat(dummyResult.getResult(), matcher); | ||
} | ||
|
||
private static Collection<Arguments> provideDummyInput() { | ||
return Arrays.asList( | ||
Arguments.of( | ||
new UnusedBean.DummyInput("whatever", new UnusedBean.NestedDummyInput(Arrays.asList(1, 2, 3))), | ||
CoreMatchers.is("whatever/6")), | ||
Arguments.of( | ||
new UnusedBean.DummyInput("hi", new UnusedBean.NestedDummyInput(Collections.emptyList())), | ||
CoreMatchers.is("hi/0"))); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
integration-tests/main/src/test/java/io/quarkus/it/main/ParameterResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.quarkus.it.main; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
import io.quarkus.it.arc.UnusedBean; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
@ExtendWith(ParameterResolverTest.UnusedBeanDummyInputResolver.class) | ||
public class ParameterResolverTest { | ||
|
||
@Inject | ||
UnusedBean unusedBean; | ||
|
||
@Test | ||
public void testParameterResolver(UnusedBean.DummyInput dummyInput) { | ||
UnusedBean.DummyResult dummyResult = unusedBean.dummy(dummyInput); | ||
assertEquals("whatever/6", dummyResult.getResult()); | ||
} | ||
|
||
public static class UnusedBeanDummyInputResolver implements ParameterResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) | ||
throws ParameterResolutionException { | ||
return UnusedBean.DummyInput.class.getName().equals(parameterContext.getParameter().getType().getName()); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, | ||
ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return new UnusedBean.DummyInput("whatever", new UnusedBean.NestedDummyInput(Arrays.asList(1, 2, 3))); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test-framework/junit5/src/main/java/io/quarkus/test/junit/internal/DeepClone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.quarkus.test.junit.internal; | ||
|
||
/** | ||
* Strategy to deep clone an object | ||
* | ||
* Used in order to clone an object loaded from one ClassLoader into another | ||
*/ | ||
public interface DeepClone { | ||
|
||
Object clone(Object objectToClone); | ||
} |
23 changes: 23 additions & 0 deletions
23
test-framework/junit5/src/main/java/io/quarkus/test/junit/internal/XStreamDeepClone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.test.junit.internal; | ||
|
||
import com.thoughtworks.xstream.XStream; | ||
|
||
/** | ||
* Super simple cloning strategy that just serializes to XML and deserializes it using xstream | ||
*/ | ||
public class XStreamDeepClone implements DeepClone { | ||
|
||
private final XStream xStream; | ||
|
||
public XStreamDeepClone(ClassLoader classLoader) { | ||
xStream = new XStream(); | ||
XStream.setupDefaultSecurity(xStream); | ||
xStream.allowTypesByRegExp(new String[] { ".*" }); | ||
xStream.setClassLoader(classLoader); | ||
} | ||
|
||
public Object clone(Object objectToClone) { | ||
final String serialized = xStream.toXML(objectToClone); | ||
return xStream.fromXML(serialized); | ||
} | ||
} |