-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Introduce extension point before test instantiation #748
Introduce extension point before test instantiation #748
Conversation
To migrate our existing framework to JUnit 5 I'd like to hook into the lifecycle of JUnit 5 test execution before any test instance is created. We want to configure the test environment for each test right before the test instance is created. * TestInstancePostProcessor is too late, as instance fields have already been initialized. * BeforeAll is not appropriate, as we need to configure the environment for each test run. --- I hereby agree to the terms of the JUnit Contributor License Agreement.
That's not necessarily true. The primary use case for a
Can you please expound on what you mean by that statement? It's too late to achieve what? |
Short story:For our test infrastructure to create test data, we must distinguish calls during the static initialization of each test class from calls during test instantiation for each test method. Long story:When we write our tests, we want to restrict the data that have to be set explicitly to those that the specific test requires. All other properties are set randomly, but valid. Naturally using random data may introduce randomly failing tests, so we need the means to easily reproduce test failures that e.g. happen in the CI environment. private static final Address staticAddress =
createRandomAddress(); // determined by the class seed
private Address instanceAddress =
createRandomAddressBuilder() // determined by the instance seed
.withStreet("some street")
.build();
@Test
void someTest() {
Address anotherAddress =
createRandomAddress(); // also determined by the instance seed
// ...
} We note the class seed in a |
IIRC there has also been some discussion about providing an extension point for test class instantiation, i.e. it would return the test class instance, but I cannot remember the issue at the moment. That would be more powerful and I think we'd need only one. @smoyer64 @junit-team/junit-lambda Do you remember the issue? |
I believe the issue I was looking for is #672. |
I fully agree. An extension point for test instance creation would certainly solve our problem and provide more flexibility than the one we proposed. |
@marcphilipp - Yes, I think #672 (a Weld use-case) is the closest match. In that issue's discussion, you reference #203 (now closed) and following the chain of issues, @jlink references #201 (a PowerMock use-case). Looking back at that issue, the @sbrannen quote:
should probably be clarified. Does "test container" refer to the top-level test container or does it refer to (at a minimum) the test's immediate parent container? I can certainly imagine scenarios where you might use different a InstanceFactory for different tests. It definitely makes sense that for each test, only one InstanceFactory can provide the actual instance. While my original question to the Weld team (performing CDI bootstrap and initialization for a provided instance) couldn't be done, I think the idea of an InstanceDecorator might also provide value ... since I have no use-cases to provide for this concept, I won't propose it (at least not yet). In general, the JUnit 4 rules that I've written have predominately been doing some form of decorating/initializing but many of those cases can be handled by TestInstancePostProcessor extensions (and perhaps all). Thanks for keeping this "on the radar", I'd love to be able to use Weld in JUnit 5 tests! |
I fully understand that the proposed InstanceFactory extension may exists only once per test (container). |
I typed a longer answer with different follow-up questions and then realized that this is a PR. I don't know how the JUnit team stands on this but I would prefer discussing the general feasibility of such an extension point on an issue and use the PR to discuss the concrete code change. @codecholeric Could you open a new issue with the feature request and the short and long story as background? |
@nicolaiparlog Done -> #839 |
I think this use case should be solvable using |
@marcphilipp I finally had a possibility to check it out. |
@codecholeric Thanks for checking! Feel free to open a new issue should this lack of composability become a problem. |
To migrate our existing framework to JUnit 5 I'd like to hook into the lifecycle of JUnit 5 test execution before any test instance is created.
We want to configure the test environment for each test right before the test instance is created.
I hereby agree to the terms of the JUnit Contributor License Agreement.
Overview
Please describe your changes here and list any open questions you might have.
I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@API
annotations