2

Right now, I have around 107 test input cases for my interpreter, and I have my JUnit tester set up to manually handle each case independently so as to not lump them all together. That is, if I use a loop to iterate over the test files as such

for (int i = 0; i < NUM_TESTS; i++) {
    String fileName = "file_" + (i + 1) + ".in";
    testFile(fileName);
}

JUnit will create one giant test result for all 107 tests, meaning if one fails, the entire test fails, which I don't want. As I said, right now I have something like

@Test
public static void test001() {
    testFile("file1.in");
}

@Test
public static void test002() {
    testFile("file2.in");
}

While this works, I imagine there's a much better solution to get what I'm after.

2 Answers 2

1

You can use @ParameterizedTest with @MethodSource annotations.

For exemple :

@ParameterizedTest
@MethodSource("fileNameSource")
void test(final String fileName) {
    testFile(fileName);
}

private static Stream<String> fileNameSource() {
    return IntStream.range(0,NUM_TESTS).mapToObj(i -> "file_" + (i + 1) + ".in");
}

Check the documentation at https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

For each params returned by fileNameSource(), the corresponding test will be considered as a different case.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. Thanks!
0

You have to define own structure based on your need, one way to define your input to json file in like list of values as below.

{
   [
     "value1",
     "value2"
   ]
}

Read this value when you test case execute with the help of object mapper.

objectMapper.readValue(fixture("filePathName.json"),CustomInput.class);

Where CustomInput would be something like below.

public class CustomInput {
    List<String> values;
}

You can keep increase & decrease your inputs in json.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.