Add unit test showing null Methodinvocation type#870
Draft
greg-dennis wants to merge 6 commits intoopenrewrite:mainfrom
Draft
Add unit test showing null Methodinvocation type#870greg-dennis wants to merge 6 commits intoopenrewrite:mainfrom
greg-dennis wants to merge 6 commits intoopenrewrite:mainfrom
Conversation
src/test/java/org/openrewrite/java/testing/cleanup/KotlinTestMethodShouldBeUnitTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/cleanup/KotlinTestMethodShouldBeUnitTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/cleanup/KotlinTestMethodShouldBeUnitTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/cleanup/KotlinTestMethodShouldBeUnitTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/cleanup/KotlinTestMethodShouldBeUnitTest.java
Outdated
Show resolved
Hide resolved
Laurens-W
reviewed
Dec 11, 2025
Comment on lines
+41
to
+79
| @Test | ||
| void showNullMethodInvocationType() { | ||
| //language=kotlin | ||
| SourceSpec<?> source = getOnly( | ||
| kotlin( | ||
| """ | ||
| class ExampleTest { | ||
| fun myTest() = run { | ||
| return 1 | ||
| } | ||
| } | ||
| """ | ||
| )); | ||
| Parser parser = KotlinParser.builder().build(); | ||
| Path sourcePath = parser.sourcePathFromSourceText(source.getDir(), source.getBefore()); | ||
| ExecutionContext ctx = new InMemoryExecutionContext(); | ||
| Parser.Input input = Parser.Input.fromString( | ||
| sourcePath, source.getBefore(), parser.getCharset(ctx)); | ||
| SourceFile sourceFile = getOnly( | ||
| parser.parseInputs(singleton(input), null, ctx).toList()); | ||
| new FindMissingTypesVisitor().visit(sourceFile, ctx); | ||
| } | ||
|
|
||
| private static class FindMissingTypesVisitor extends KotlinIsoVisitor<ExecutionContext> { | ||
|
|
||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, | ||
| ExecutionContext ctx) { | ||
| assertNotNull(method.getMethodType()); | ||
| return method; | ||
| } | ||
| } | ||
|
|
||
| private static <T> T getOnly(Iterable<T> iterable) { | ||
| Iterator<T> iter = iterable.iterator(); | ||
| T only = iter.next(); | ||
| assertFalse(iter.hasNext()); | ||
| return only; | ||
| } |
Contributor
There was a problem hiding this comment.
Suggested change
| @Test | |
| void showNullMethodInvocationType() { | |
| //language=kotlin | |
| SourceSpec<?> source = getOnly( | |
| kotlin( | |
| """ | |
| class ExampleTest { | |
| fun myTest() = run { | |
| return 1 | |
| } | |
| } | |
| """ | |
| )); | |
| Parser parser = KotlinParser.builder().build(); | |
| Path sourcePath = parser.sourcePathFromSourceText(source.getDir(), source.getBefore()); | |
| ExecutionContext ctx = new InMemoryExecutionContext(); | |
| Parser.Input input = Parser.Input.fromString( | |
| sourcePath, source.getBefore(), parser.getCharset(ctx)); | |
| SourceFile sourceFile = getOnly( | |
| parser.parseInputs(singleton(input), null, ctx).toList()); | |
| new FindMissingTypesVisitor().visit(sourceFile, ctx); | |
| } | |
| private static class FindMissingTypesVisitor extends KotlinIsoVisitor<ExecutionContext> { | |
| @Override | |
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, | |
| ExecutionContext ctx) { | |
| assertNotNull(method.getMethodType()); | |
| return method; | |
| } | |
| } | |
| private static <T> T getOnly(Iterable<T> iterable) { | |
| Iterator<T> iter = iterable.iterator(); | |
| T only = iter.next(); | |
| assertFalse(iter.hasNext()); | |
| return only; | |
| } | |
| @Override | |
| public void defaults(RecipeSpec spec) { | |
| spec | |
| .parser(KotlinParser.builder()); | |
| } | |
| @Test | |
| void showNullMethodInvocationType() { | |
| rewriteRun( | |
| //language=kotlin | |
| kotlin( | |
| """ | |
| class ExampleTest { | |
| fun myTest() = run { | |
| return 1 | |
| } | |
| } | |
| """ | |
| )); | |
| } |
I've simplified it to this and am indeed seeing it fail in rewrite-testing-frameworks but succeed in rewrite
Contributor
Author
There was a problem hiding this comment.
Right, that simple example will show the symptom, but I found the failure to be more cryptic. The intention of the PR was to reveal the underlying issue: that the type of the run method invocation is null.
Contributor
Author
There was a problem hiding this comment.
@Laurens-W, there appears to be other code that "expects" the types of Kotlin method invocations to sometimes be null (example). If that is sometimes "expected" behavior, the following workaround might make sense: openrewrite/rewrite#6409
Thoughts?
src/test/java/org/openrewrite/java/testing/cleanup/KotlinTestMethodShouldBeUnitTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/cleanup/KotlinTestMethodShouldBeUnitTest.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's changed?
What's your motivation?
Add unit test showing null Methodinvocation type. This test fails in this repo but the very same test passes when added to the
rewriterepo.Anything in particular you'd like reviewers to focus on?
Anyone you would like to review specifically?
Have you considered any alternatives or workarounds?
Any additional context
Checklist