Support JSON as source for parameterized tests arguments
See original GitHub issueImplement an ArgumentConverter
that parses JSON and an ArgumentSource
that parses JSON files and passes them as arguments to a parameterized test.
This is pretty straightforward for a single file and parameter:
@ParameterizedTest
@JsonSource("customers.json")
void testCustomer(Customer customer) { /*...*/ }
It’s a little unclear how to parse files for two parameters, though. There could be one file per parameter…
@ParameterizedTest
@JsonSource({ "customers.json", "contracts.json" })
void testCustomer(Customer customer, Contract contract) { /*...*/ }
… but then customer/contract pairs would be far apart and hard to identify by looking at the files.
Alternatively, there could be a top-level container…
{
customer: { /*...*/ }
contract: { /*...*/ }
}
… and the parser takes them apart when resolving parameter:
@ParameterizedTest
@JsonSource({ "customers-contracrs.json" })
void testCustomer(Customer customer, Contract contract) { /*...*/ }
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
JSON Argument Source - JUnit Pioneer
The JSON argument sources let you provide arguments for parameterized tests from JSON. There are three annotations: ... There are various ways how...
Read more >Parameterized Tests Using JSON File As Input Source - Medium
Parameterized Tests Using JSON File As Input Source · 1- Dependencies. First thing first, lets discuss the required dependencies. · 2- Simple Unit...
Read more >junit-json-params | JUnit 5 JSON Parameterized Tests library
junit-json-params. A Junit 5 library to provide annotations that load data from JSON Strings or files in parameterized tests.
Read more >Guide to JUnit 5 Parameterized Tests - Baeldung
This feature enables us to execute a single test method multiple times with different parameters. In this tutorial, we're going to explore ...
Read more >JUnit 5 Tutorial: Writing Parameterized Tests - Petri Kainulainen
The @ValueSource annotation is the simplest argument source that's supported by JUnit 5. However, JUnit 5 support other argument sources as ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I like your approach, @filiphr. Having a single JS object per invocation makes more sense than what I proposed above. I also like deconstruction in
testCustomerFields
. 👍🏾Regarding @aepfli’s proposal for default data, I think we should not implement that in the first try but that raises the question whether we already want to prepare for such a scenario by requiring the actual data on a top-level
data
field. That may be needlessly cumbersome for the 9x% of cases that only use data, though. To address this, we could give@JsonSource
an optional argument that specifies the attribute name that contains the value or value array, e.g:It could default to a constant
ROOT
that specifies the data is in the document’s root.I 100% agree with your
@JsonSource
argument: It should accept a string (or array thereof - that looks good, too!) asvalue
, so the attribute doesn’t need to be specified.Regarding two-vs-three annotations, I definitely like three annotations, i.e. one per use case, better. It seems cleaner and makes those easier to use as well (because they can rely on
value
). First question: Do you agree?Then of course, there’s the symmetry with JUnit Jupiter, which is definitely nice to have as it makes it easier for users to adopt these features. Weighing ease-of-learning (two annotations) against ease-of-use (three annotations), I lean towards the latter and would go with three annotations. Second question: What do you think?
If both answers are on line with mine, you can just go ahead and refactor accordingly. Otherwise let’s
fightdiscuss.