JSON Driven JUnit Testing

Purushottam Sinha
2 min readOct 31, 2024

--

Photo by Ferenc Almasi on Unsplash

In today’s fast-paced development environment, ensuring the reliability of software through thorough testing is crucial. JSON Driven Testing (JDT) is a robust approach, allowing testers to define scenarios, inputs, and expected outputs in a straightforward and structured manner. This blog post delves into the fundamentals of JSON driven testing, its structure, and integration with JUnit for effective testing.

What is JSON Driven Testing?

JSON Driven Testing is a testing strategy where test cases are defined in JSON format. This structured approach enables users to create, manage, and execute test scenarios without hard-coding them in the testing framework. This method enhances readability and maintainability, enabling teams to collaborate more effectively.

The Structure of JSON for Testing

{
"TestingSuiteCase1": [
{
"scenario": "scenario1",
"query": {
"param1": 1,
"param2": "value1",
"param3": "2020-01-01T00:00:00Z"
}
"response": {
"answer": 2
}
},
{
"scenario": "scenario2",
"query": {
"param1": 8,
"param2": "value5",
"param3": "2022-05-01T00:00:00Z"
}
"response": {
"answer": 7
}
],
"TestingSuiteCase2": [
{
"scenario": "scenario3",
"query": {
"param1": 2,
"param2": "value2",
"param3": "2024-01-01T00:00:00Z"
}
"response": {
"answer": 3
}
}
]
}

TestingSuiteCase: Umbrella of related test cases like for a given functionality

  • scenario: Describes the test scenario.
  • query: Contains the input parameters for the test to be casted into Schedule.
  • response: Expected output of the test.

JUnit Structure

BeforeAll

Load test cases from json file and create the report folder if not present.

@BeforeAll
static void setUpBeforeClass() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
testCases_source =
(Map<String, ArrayList<Object>>)
objectMapper.readValue(
new File(
"src/test/resources/junit-test-resource/test.json"),
Map.class);
File reportDir = new File("src/test/resources/report");
if (!reportDir.exists()) {
reportDir.mkdirs();
}
}

AfterAll

Prints if the test was successful and the test with failed if not. A suite stops after one test fails.

@AfterAll
static void after_tests() {
try (FileWriter writer =
new FileWriter("src/test/resources/report/test_report.txt", true)) {
TestFrameworkUtils.testSuiteCompletedLog(writer, testPassed, currentTestScenario);
} catch (IOException e) {
throw new RuntimeException(e);
}
testCases_source.clear();
}

Executing the test

testObjtry (FileWriter writer =
new FileWriter("src/test/resources/report/test_report.txt")) {
TestFrameworkUtils.testModuleRunningLog(writer, "MyMockClass");

TestFrameworkUtils.testCasesRunningLog(
writer, "MyMockClass.function1");
testCases_source
.get("Function1")
.forEach(
obj -> {
currentTestScenario = TestFrameworkUtils.openingLog(writer, obj);
inputObj = new Schedule();
TestObj testObj = ObjectConverter.convertObject(obj, TestObj.class);
inputObj = CommonTestUtils.createInputObj(testObj, timeNow);
Response expectedResponse =
CommonTestUtils.createResponse(testObj, timeNow);


Response response =
myMockClass.function1(inputObj);
assertEquals(expectedResponse, response);
TestFrameworkUtils.closingLog(writer, currentTestScenario);
});
}

--

--

No responses yet