Search code examples
intellij-ideaopenrewrite

Openrewrite: Run unit test in the IDE (IntelliJ) that uses classes from the same multiproject (not packages in a jar)


My unit test in optaplanner-rewrite has this code using OpenRewrite bom 1.11.0:

@Override
public void defaults(RecipeSpec spec) {
    spec.recipe(new AsConstraintBuilder())
            .parser(JavaParser.fromJavaVersion()
                    .classpath("optaplanner-core"));
}

and it runs fine in Maven.

But when I run the unit test in my IDE (IntelliJ), I get this error:

java.lang.IllegalArgumentException: Unable to find runtime dependencies beginning with: 'optaplanner-core'

    at org.openrewrite.java.JavaParser.dependenciesFromClasspath(JavaParser.java:97)
    at org.openrewrite.java.JavaParser$Builder.classpath(JavaParser.java:250)

This is because my IDE window has a multiproject open, that includes both optaplanner-core and optaplanner-rewrite. There is no optaplanner-core.jar in the classpath of my test run, only optaplanner-core/target/classes.

How do I run my unit test in IntelliJ?


Solution

  • This is not a proper solution, but as a workaround you should be able to load the entire classpath like this:

        @Override
        public void defaults(RecipeSpec spec) {
            List<Path> classpath = new ClassGraph().getClasspathURIs().stream().map(Paths::get).toList();
            spec.recipe(new AsConstraintBuilder())
                .parser(JavaParser.fromJavaVersion()
                        .classpath(classpath));
        }