I have below maven java projects:
Validator project contains the validation code and the jar is used by demoModel1 and demoModel2 projects to perform validation. To perform validation, in demoModel1 and demoModel2 projects, I have to write JUnit test cases, but those test cases are similar, except different index for them as parameters. Because of that reason, I have created a parameterized test class named ExecuteSimulation:
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
public class ExecuteSimulation implements ArgumentsProvider {}
For now, I have to create this ExecuteSimulation class in both demoModel1 and demoModel2 projects. But I want to reuse the ExecuteSimulation class in both demoModel1 and demoModel2 projects. I tried to put ExecuteSimulation class in com.validator project. If I put the class in the src/java folder of com.validator project, it yields error, complaining that junit dependencies are not found.
If I put ExecuteSimulation class in the src/test folder of com.validator project and try to use it in demoModel1 and demoModel2, both of the project yields errors complaining that the reference of ExecuteSimulation class from com.validator project is not found. I guess it happens because of the location src/test.
In this regard, what will be the proper way to reuse this parameterized test class across different maven java projects?
What you're trying to achieve is described in the Maven JAR plugin documentation ("how to create a jar containing test classes"). You have basically two options:
jar
file with the test classes of an existing module (test-jar
build based on src/test
).src/java
in this module).Both approaches will yield a jar
file that should be imported as a dependency in other modules with test
scope. If you decide to go with the second approach, the dependencies like JUnit will not have a test
scope in this new module, but they will be used only with this scope in the modules importing them.