Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.cleanup;

import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Parser;
import org.openrewrite.SourceFile;
import org.openrewrite.java.tree.J;
import org.openrewrite.kotlin.KotlinIsoVisitor;
import org.openrewrite.kotlin.KotlinParser;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import java.nio.file.Path;
import java.util.Collections;
import java.util.Iterator;

import static java.util.Collections.singleton;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.openrewrite.kotlin.Assertions.kotlin;

//
public class KotlinTestMethodShouldBeUnitTest implements RewriteTest {

@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;
}
Comment on lines +41 to +79
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

@greg-dennis greg-dennis Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

}