can i get help with this ?
this my the json file :
{
"1": "1",
"2": "2",
"3": "Fizz",
"4": "4",
"5": "Buzz",
"6": "Fizz",
"7": "7",
"8": "8",
"9": "Fizz",
"10": "Buzz",
"11": "11",
"12": "Fizz",
"13": "13",
"14": "14",
"15": "FizzBuzz"
}
I added some dependencies in pom.xml
required for json ,
fortunately test passes.
This is the test case and result:
@DisplayName("TESTING WITH SMALL JSON FILE")
@ParameterizedTest()
@JsonFileSource(resources = "/small-test-data.json")
@Order(-1)
void testSmallDataJsonFile(JsonObject jsonObject) {
jsonObject.forEach((k,v)-> {
assertEquals(jsonObject.getString(k),FizzBuzz.compute(Integer.parseInt(k)));
log.info("✔ actual={},expected={}",v,FizzBuzz.compute(Integer.parseInt(k)));
});
}
ONLY THE RESOURCE DATA IS SHOWN AS PARMETER
I 've used custom logger to print the test result.
How can I pass parameters (actual , expected) corresponding to key , value
for my test using the @ParameterizedTest()
annotation?
To pass parameters (in this case, actual and expected) corresponding to the key and value pairs from your JSON file to a parameterized test in JUnit 5, you will need to make use of the @MethodSource or @CsvSource annotation instead of @JsonFileSource. Unfortunately, @JsonFileSource is not a standard annotation in JUnit 5, so you might be using a custom extension or a third-party library.
Since JSON files are not natively supported as sources for parameterized tests in JUnit 5, you will have to write a method that parses your JSON file and returns a stream of arguments. Here is a step-by-step guide on how you can achieve this:
First, add the necessary dependencies to your pom.xml to handle JSON parsing, if not already present:
Then, implement the method to parse the JSON and the test method:
In this example: