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:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>YOUR_VERSION</version>
</dependency>
Then, implement the method to parse the JSON and the test method:
import org.json.JSONObject;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.api.DisplayName;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
class YourTestClass {
static Stream<Arguments> provideJsonData() throws IOException {
String filePath = "src/test/resources/small-test-data.json";
String content = new String(Files.readAllBytes(Paths.get(filePath)));
JSONObject jsonObject = new JSONObject(content);
return jsonObject.keySet().stream()
.map(key -> Arguments.of(key, jsonObject.getString(key)));
}
@DisplayName("TESTING WITH SMALL JSON FILE")
@ParameterizedTest
@MethodSource("provideJsonData")
void testSmallDataJsonFile(String key, String value) {
assertEquals(value, FizzBuzz.compute(Integer.parseInt(key)));
// Replace 'log' with your logging implementation
log.info("✔ actual={}, expected={}", value, FizzBuzz.compute(Integer.parseInt(key)));
}
}
In this example: