question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support JSON as source for parameterized tests arguments

See original GitHub issue

Implement 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:closed
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
nipafxcommented, May 29, 2021

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:

ParameterizedTest
@JsonSource(value = "customers.json", data = "the-customers")
void testCustomer(Customer customer) { /*...*/ }
{
	"the-customers": [
		{ ... }
	]
}

It could default to a constant ROOT that specifies the data is in the document’s root.

0reactions
nipafxcommented, Mar 10, 2022

I 100% agree with your @JsonSource argument: It should accept a string (or array thereof - that looks good, too!) as value, 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 fight discuss.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found