Skip to content

Commit

Permalink
Remove throws from method that adopts assertThrows (#564)
Browse files Browse the repository at this point in the history
* Remove throws from method that adopts `assertThrows`

* Just remove the entire throws for tests

* Just remove the entire throws for tests

* Remove thrown exceptions in ExpectedExceptionToAssertThrows
  • Loading branch information
timtebeek committed Aug 8, 2024
1 parent 519ebcb commit ebd24fe
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
package org.openrewrite.java.testing.junit5;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
Expand All @@ -27,6 +24,7 @@
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;

import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -178,6 +176,13 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
statement
);

// Clear out any declared thrown exceptions
List<NameTree> thrown = m.getThrows();
if (thrown != null && !thrown.isEmpty()) {
assert m.getBody() != null;
m = m.withBody(m.getBody().withPrefix(thrown.get(0).getPrefix())).withThrows(Collections.emptyList());
}

maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows");

if (expectMessageMethodInvocation != null && !isExpectMessageArgAMatcher && m.getBody() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markup;

import java.util.Collections;
import java.util.Comparator;
import java.util.Set;

Expand Down Expand Up @@ -149,6 +150,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
.staticImports("org.junit.jupiter.api.Assertions.assertThrows")
.build()
.apply(updateCursor(m), m.getCoordinates().replaceBody(), cta.expectedException, lambda);
m = m.withThrows(Collections.emptyList());
maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void defaults(RecipeSpec spec) {
.recipe(new ExpectedExceptionToAssertThrows());
}

@DocumentExample
@Test
void leavesOtherRulesAlone() {
//language=java
Expand Down Expand Up @@ -71,6 +70,7 @@ class MyTest {
);
}

@DocumentExample
@Test
void expectedExceptionRule() {
//language=java
Expand Down Expand Up @@ -420,4 +420,53 @@ public void expectExceptionUseCases() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/563")
void expectedCheckedExceptionThrowsRemoved() {
//language=java
rewriteRun(
java(
"""
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
class MyTest {
@Rule
ExpectedException thrown = ExpectedException.none();
@Test
public void testEmptyPath() throws IOException{
this.thrown.expect(IOException.class);
foo();
}
void foo() throws IOException {
throw new IOException();
}
}
""",
"""
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.Test;
class MyTest {
@Test
public void testEmptyPath() {
assertThrows(IOException.class, () -> foo());
}
void foo() throws IOException {
throw new IOException();
}
}
"""
)
);
}
}
Loading

0 comments on commit ebd24fe

Please sign in to comment.