Skip to content

TestMethodsShouldBeVoid should ignore test factory junit functions #934

@barbulescu

Description

@barbulescu

What version of OpenRewrite are you using?

I am using latest version

How are you running OpenRewrite?

Running unit test in rewrite-testing-frameworks

What is the smallest, simplest way to reproduce the problem?

    @Override
    public void defaults(RecipeSpec spec) {
        spec
          .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
            "junit-jupiter-api-5", "junit-jupiter-params-5"))
          .parser(KotlinParser.builder().classpathFromResources(new InMemoryExecutionContext(),
            "junit-jupiter-api-5", "junit-jupiter-params-5"))
          .recipe(new TestMethodsShouldBeVoid());
    }


    @Test
    void testFactoryMustReturnAValueInKotlin() {
        rewriteRun(
          //language=kotlin
          kotlin(
            """
              import org.junit.jupiter.api.DynamicTest
              import org.junit.jupiter.api.TestFactory

              class Doer {
                  @TestFactory
                  fun createSomeTests() : List<DynamicTest> {return listOf<DynamicTest>()}
              }
              """
          )
        );
    }

    @Test
    void testFactoryMustReturnAValueInJava() {
        rewriteRun(
          //language=java
          java(
            """
              import org.junit.jupiter.api.DynamicTest;
              import org.junit.jupiter.api.TestFactory;
              import java.util.List;
              import java.util.ArrayList;

              class Doer {
                  @TestFactory
                  List<DynamicTest> createSomeTests() {
                      return new ArrayList<DynamicTest>();
                  }
              }
              """
          )
        );
    }

What did you expect to see?

I would expect to see no change because functions annotated with TestFactory must return a value.

What did you see instead?

In case of Kotlin test, an exception was thrown (see below) and in case of Java test:

import org.junit.jupiter.api.DynamicTest;
  import org.junit.jupiter.api.TestFactory;
  import java.util.List;
  import java.util.ArrayList;
  
  class Doer {
      @TestFactory
      void createSomeTests() {
          new ArrayList<DynamicTest>();
      }
  }

What is the full stack trace of any errors you encountered?

Caused by: java.lang.ClassCastException: class org.openrewrite.java.tree.J$MethodInvocation cannot be cast to class org.openrewrite.java.tree.J$Return (org.openrewrite.java.tree.J$MethodInvocation and org.openrewrite.java.tree.J$Return are in unnamed module of loader 'app')
	at org.openrewrite.kotlin.KotlinVisitor.visitReturn(KotlinVisitor.java:234)
	at org.openrewrite.kotlin.tree.K$Return.acceptKotlin(K.java:1589)
	at org.openrewrite.kotlin.tree.K.accept(K.java:57)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:242)
	... 71 more

Are you interested in contributing a fix to OpenRewrite?

Yes, I will send a PR later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions