Is it possible to write an Openrewrite java recipe test ignoring compilation errors due to unknown class types not included in JavaParser
classpath?
Example:
@Test
void requiredRequestParam() {
rewriteRun(
spec -> spec
.recipe(new MandatoryRequestParameter())
.parser(JavaParser.fromJavaVersion().classpath("spring-web")),
java(
"""
import org.springframework.web.bind.annotation.RequestParam;
class ControllerClass {
public String sayHello (
@RequestParam(value = "fred") String fred,
@RequestParam(value = "lang") String lang,
@RequestParam(value = "aNumber") Long aNumber
) {
return "Hello";
}
}
""",
"""
import org.springframework.web.bind.annotation.RequestParam;
class ControllerClass {
public String sayHello (
@RequestParam(required = true, value = "fred") String fred,
@RequestParam(value = "lang") String lang,
@RequestParam(required = true, value = "aNumber") Long aNumber
) {
return "Hello";
}
}
"""
)
);
}
In this case, spring-web
artifact must be included in test classpath for the org.springframework.web.bind.annotation.RequestParam
type to be recognized.
You can call typeValidationOptions()
on the RecipeSpec
for this purpose (see example below). To disable all type checks you can call typeValidationOptions(TypeValidation.none())
and to be more fine-grained, TypeValidation
provides a builder API to disable specific type checks.
As pointed out by Tim however, you would normally want to configure the recipe classpath (using either JavaParser#classpath()
or JavaParser#classpathFromResources()
, see docs for details) so that you don't get any type validation errors. Disabling type validation errors can still be necessary in some situations (e.g. when you want to check that your recipe also does something useful in the presence of type errors).
@Test
void requiredRequestParam() {
rewriteRun(
spec -> spec
.recipe(new MandatoryRequestParameter())
.parser(JavaParser.fromJavaVersion().classpath("spring-web"))
.typeValidationOptions(TypeValidation.none()),
java(
// ...
)
);
}