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

Create simple aggregator #745

Closed
Michael1993 opened this issue Jul 14, 2023 · 2 comments
Closed

Create simple aggregator #745

Michael1993 opened this issue Jul 14, 2023 · 2 comments

Comments

@Michael1993
Copy link
Member

Michael1993 commented Jul 14, 2023

While working on #742, I had an idea for a simple ArgumentAggregator. Imagine if you could write the following test:

@ParameterizedTest
@CsvSource({
    "Jane Doe, 51, F",
    "John Doe, 50, M"
})
void testPeople(@Aggregate Person person) {
    // some assertions
}

So I created #744, which does exactly that. 😊 It comes with a couple stipulations but it covers the basic use case: you have an object with a constructor and some arguments (same order) and you want to create an object from them.

I'm not sure this has any merit as it's very simple to either use ArgumentAccessor or write your own ParameterResolver or even ArgumentsAggregator instead. Thoughts?

// P.S.: This is a rather trivial thing both to think of and to write so let me know if this already exists somewhere - I did not look.

@Bukama
Copy link
Member

Bukama commented Jul 15, 2023

I like really like the idea as it's makes writing tests more maintainable when you don't have to call the constructor in every test.

What I can't see or understand is how you decide which constructor do you choose, meaning how do you decide what types the values are. When I understand you draft correctly you treat them all as Strings and then maybe try to turn them into primitives (like the age here). But I do not (yet) understand where you transform the gender into the Gender class in your draft or how you would transform the birthday into a LocalDate (if we have a look at the JUnit example, where the class is passed to the ArgumentsAccessor). To nail down the question I still have in mind: (How) Can (Do) we support non primitive/non Strings constructors?

Second quests would be: Does the same thing works with records? We are on 11 but many apps, which use Pioneer are on newer versions. I imagine it works out of the box as with data projection with JPA, but we might want to test that (at least once, not with a current test due Java version - and if it fails maybe file a ticket for when we upgrade to Java 17+)

@Michael1993
Copy link
Member Author

enum, LocalDate, LocalTime and LocalDateTime mapping works out of the box because we use ArgumentsAccessor::get(int, Class).

[...] In such cases, an ArgumentsAccessor can be used instead of multiple parameters. Using this API, you can access the provided arguments through a single argument passed to your test method. In addition, type conversion is supported as discussed in Implicit Conversion.

This solution does not work for more complex types (i.e.: you have another object as a field in your parameter) but we already have JSON source supporting that use-case (and maybe we should consider adding YAML? ¯\_(ツ)_/¯ ).

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