rewrite-spring
rewrite-spring copied to clipboard
SpringBoot2JUnit4to5Migration handling of ExpectedException results in invalid code
What version of OpenRewrite are you using?
I am using openrewrite-maven-plugin 5.11.0 with rewrite-spring 5.1.1
How are you running OpenRewrite?
I am using the Maven plugin. Apologies, my project is on an offline network with no Internet access, so I'm not able to share or copy anything.
This is the gist of the initial code
import my.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 {
final String EXPECTED_MSG = "this is what i expected";
expectedException.expect(MyAppException.class);
expectedException.expectMessage(EXPECTED_MSG);
doThrow(new MyAppException(EXPECTED_MSG)).when(...)...
// some app code expected to throw exception here
}
}
What did you expect to see?
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import my.MyAppException;
import org.junit.jupiter.api.Test;
class ApplicationTest {
@Test
void sampleTest() throws Exception {
final String EXPECTED_MSG = "this is what i expected";
ThrowableException ex = assertThrows(MyAppException.class, () -> {
doThrow(new MyAppException(EXPECTED_MSG)).when(...)...
// some app code expected to throw exception here
});
assertTrue(ex.getMessage().contains(EXPECTED_MSG));
}
}
What did you see instead?
import static org.junit.jupiter.api.Assertions.assertEquals;
// the import for assertThrows was missing
import my.MyAppException;
import org.junit.jupiter.api.Test;
class ApplicationTest {
@Test
void sampleTest() throws Exception {
ThrowableException ex = assertThrows(MyAppException.class, () -> { // ERROR: assertThrows is undefined (missing import)
final String EXPECTED_MSG = "this is what i expected"; // this shouldn't have been put inside the lambda expression
doThrow(new MyAppException(EXPECTED_MSG)).when(...)...
// some app code expected to throw exception here
});
assertTrue(ex.getMessage().contains(EXPECTED_MSG)); // ERROR: EXPECTED_MSG is undefined here
}
}
What is the full stack trace of any errors you encountered?
No errors. The rewrite completed "successfully" but just produced code with syntax errors.
Notes
This bug is in the same context and similar conditions as Issue #363 but seems to be a different problem.
Are you interested in contributing a fix to OpenRewrite?
Wish I could. Sorry, not able at this time.