rewrite-spring icon indicating copy to clipboard operation
rewrite-spring copied to clipboard

SpringBoot2JUnit4to5Migration produces uncompilable code on ExpectedException rule with assertions on exception

Open avmohan opened this issue 2 years ago • 3 comments

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v4.46.0
  • Maven/Gradle plugin v4.46.0
  • rewrite-spring v4.36.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>4.46.0</version>
        <configuration>
          <activeRecipes>
            <recipe>org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration</recipe>
          </activeRecipes>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-spring</artifactId>
            <version>4.36.0</version>
          </dependency>
        </dependencies>
      </plugin>

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

package in.avmohan.myapp;

import static in.avmohan.myapp.exception.MyAppError.DEFAULT_ERROR;

import in.avmohan.myapp.exception.MyAppException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ApplicationTest {

  @Rule
  public ExpectedException expectedException = ExpectedException.none();

  @Test
  public void sampleTest() throws Exception {
    expectedException.expect(MyAppException.class);
    expectedException.expectMessage(DEFAULT_ERROR.getMessage());
    throw MyAppException.builder().error(DEFAULT_ERROR).build();
  }
}

What did you expect to see?

package in.avmohan.myapp;

import static in.avmohan.myapp.exception.MyAppError.DEFAULT_ERROR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import in.avmohan.myapp.exception.MyAppException;
import org.junit.jupiter.api.Test;

class ApplicationTest {

  @Test
  void sampleTest() throws Exception {
    String message = assertThrows(MyAppException.class, () -> {
      throw MyAppException.builder().error(DEFAULT_ERROR).build();
    }).getMessage();
    assertEquals(DEFAULT_ERROR.getMessage(), message);
  }
}

What did you see instead?

package in.avmohan.myapp;

import static in.avmohan.myapp.exception.MyAppError.DEFAULT_ERROR;

import in.avmohan.myapp.exception.MyAppException;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;

class ApplicationTest {

  @Test
  void sampleTest() throws Exception {
    assertThrows(MyAppException.class, () -> {
      expectedException.expectMessage(DEFAULT_ERROR.getMessage());
      throw MyAppException.builder().error(DEFAULT_ERROR).build();
    });
  }
}

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

There were no errors. mvn rewrite:run ran to success, but code produced was uncompilable.

avmohan avatar May 28 '23 09:05 avmohan

The methodType of method invocation expectedException.expectMessage(DEFAULT_ERROR.getMessage()) is coming as null. So the statement is not filtered out by in bodyWithoutExpectedExceptionCalls in ExpectedExceptionToAssertThrowsVisitor#visitMethodDeclaration

Screenshot 2023-05-29 at 9 44 41 AM

Screenshot 2023-05-29 at 9 45 20 AM

If instead of expectedException.expectMessage(DEFAULT_ERROR.getMessage());, it was expectedException.expectMessage("default error");, it is being parsed correctly and methodType is not null.

Screenshot 2023-05-29 at 9 51 34 AM

Not sure why the methodType is being parsed wrong as null in the first case though.

avmohan avatar May 29 '23 04:05 avmohan

I've submitted Issue openrewrite/rewrite-testing-frameworks#572 which is another bug, but dealing with the same rule and similar context, so it would probably be convenient to look at both at the same time.

TomChatt2 avatar Dec 06 '23 19:12 TomChatt2

Hi @avmohan @TomChatt2 Recently have migrated my personal project from JUnit4 to JUnit5 using SpringBoot2JUnit4to5Migration recipe and have used the following dependencies with the versions rewrite-maven-plugin as 5.27.2 rewrite-spring as 5.7.0

In fact, it was migrated successfully without any issues, infact assertThrows static import added successfully without any issues

Could you please try with the following. For more details, please go through the below link https://docs.openrewrite.org/recipes/java/spring/boot2/springboot2junit4to5migration

I hope that helps, am happy to assist you further. Thanks, Mahi

bsmahi avatar Apr 10 '24 02:04 bsmahi