Json resource value with @ParmetrizedTest , how to add arguments as key value

401 views Asked by At

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?

1

There are 1 answers

2
Ben Roberts On BEST ANSWER

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:

  1. Parse the JSON File: Create a method that reads the JSON file, parses it, and returns a Stream of Arguments. Each Arguments object should contain a pair of key and value from your JSON file.
  2. Use @MethodSource Annotation: Use this method as the source for your parameterized test by annotating your test method with @MethodSource("methodName"). Here's an example implementation:
    First, add the necessary dependencies to your pom.xml to handle JSON parsing, if not already present:
<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:

  • provideJsonData reads and parses the JSON string (you can modify it to read from a file).
  • The test method testSmallDataJsonFile is now a parameterized test that receives a key and a value from the JSON object.
  • Make sure FizzBuzz.compute and log are properly defined in your context.