0

I have created below JUnit5 parameterized test with ArgumentsSource for loading arguments for the test:

public class DemoModelValidationTest {

public ParamsProvider paramsProvider;

public DemoModelValidationTest () {
    try {
         paramsProvider = new ParamsProvider();
    }
    catch (Exception iaex) {
        
    }
}
    
@ParameterizedTest
@ArgumentsSource(ParamsProvider.class)
void testAllConfigurations(int configIndex, String a) throws Exception {
  paramsProvider.executeSimulation(configIndex);
}  

}

and the ParamsProvider class looks like below:

public class ParamsProvider implements ArgumentsProvider {
public static final String modelPath = System.getProperty("user.dir") + File.separator + "demoModels";

YAMLDeserializer deserializedYAML;
MetaModelToValidationModel converter;
ValidationRunner runner;
List<Configuration> configurationList;
List<Arguments> listOfArguments;

public ParamsProvider() throws Exception {
    configurationList = new ArrayList<>();
    listOfArguments = new LinkedList<>();
    deserializedYAML = new YAMLDeserializer(modelPath);
    deserializedYAML.load();

    converter = new MetaModelToValidationModel(deserializedYAML);
    runner = converter.convert();
    configurationList = runner.getConfigurations();

    for (int i = 0; i < configurationList.size(); i++) {
        listOfArguments.add(Arguments.of(i, configurationList.get(i).getName()));
    }
}

public void executeSimulation(int configListIndex) throws Exception {
    final Configuration config = runner.getConfigurations().get(configListIndex);
    runner.run(config);
    runner.getReporter().consolePrintReport();
}

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {

    return listOfArguments.stream().map(Arguments::of);
    //      return Stream.of(Arguments.of(0, "Actuator Power"), Arguments.of(1, "Error Logging"));

}}

In the provideArguments() method, the commented out code is working fine, but the first line of code

listOfArguments.stream().map(Arguments::of)

is returning the following error:

org.junit.platform.commons.PreconditionViolationException: Configuration error: You must configure at least one set of arguments for this @ParameterizedTest

I am not sure whether I am having a casting problem for the stream in provideArguments() method, but I guess it somehow cannot map the elements of listOfArguments to the stream, which can finally take the form like below:

Stream.of(Arguments.of(0, "Actuator Power"), Arguments.of(1, "Error Logging"))

Am I missing a proper stream mapping of listOfArguments?

1 Answer 1

2

provideArguments(…) is called before your test is invoked.

Your ParamsProvider class is instantiated by JUnit. Whatever you’re doing in desiralizeAndCreateValidationRunnerInstance should be done in the ParamsProvider constructor.

Also you’re already wrapping the values fro deserialised configurations to Arguments and you’re double wrapping them in providesArguments.

Do this:

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {

return listOfArguments.stream();

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

6 Comments

Moved the code to constructor and getting following error: Error converting parameter at index 0: No implicit conversion to convert object of type org.junit.jupiter.params.provider.Arguments$$Lambda$397/0x00000000639b9368 to type java.lang.Integer
Post all of your code
Updated with full code
You’re double wrapping the arguments. I updated my answer.
I am already doing the same thing, what's the modification here?
|

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.