Skip to content
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

Extend cartesian tests with parameterized record #772

Open
nipafx opened this issue Oct 6, 2023 · 5 comments
Open

Extend cartesian tests with parameterized record #772

nipafx opened this issue Oct 6, 2023 · 5 comments

Comments

@nipafx
Copy link
Member

nipafx commented Oct 6, 2023

When writing a set of cartesian tests, there are cases where all test methods need the same set of arguments. Instead of configuring the argument sets for the cartesian product for each test method separately, the methods could accept a record whose components are annotated accordingly. E.g.:

@CartesianTest
void test(Parameter params){
    // ...
}

record Parameter(
        @Values(ints = { 1, 2 }) int number,
        @Values(strings = { "A", "B" }) String letter) {
}

Implementing this feature would require Java 16+, which is when records were introduced. While it would probably be possible to work around the additions to the reflection API, the @Values annotation would also need to be changed to target RECORD_COMPONENT, which was of course also added in 16.

@nipafx
Copy link
Member Author

nipafx commented Oct 6, 2023

A workaround for such tests could be:

  • put all test methods with the same argument set in a (nested) test class of their own
  • parameterize its constructor arguments and assign the values to fields
  • in the test methods, use the fields

Is that currently possible or can it be feasibly implemented? // cc @Michael1993

@beatngu13
Copy link
Member

Related (I guess):

junit-team/junit5#878

Would it make sense to wait for Jupiter's design decision(s) for @ParameterizedTest?

@nipafx
Copy link
Member Author

nipafx commented Oct 9, 2023

That issue is six years old and even though there have been some promising messages recently, those are also already six months old. I'm ok with us trying our own thing and, if Jupiter comes up with something better that we could adopt, abandoning it.

@nipafx
Copy link
Member Author

nipafx commented Oct 9, 2023

Hm, there may be a workaround for the records / Java 17 problem: Just use plain classes. If a cartesian test has a parameter without annotation (or maybe some special annotation), we check that class's constructor(s) and each constructor where all arguments have an annotation that the extension understands, gets folded into the product.

That would mean records would work if a non-compact constructor is added. Not ideal, but workable.

@Michael1993
Copy link
Member

I think this can be feasibly implemented, yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants