Skip to content

Commit b86ddfd

Browse files
Meijuhtimtebeek
andauthored
Add test that generated invalid code (#331)
* Add test that generated invalid code * Whitelist the case where assertThrows can be rewritten A check is added to see if assertThrows appears immediately inside a J.Block. * Add degenerate test case showing we need to check for J.Block * Link to PR for background --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent a6cecb1 commit b86ddfd

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ private Supplier<JavaParser> assertionsParser(ExecutionContext ctx) {
7272
@Override
7373
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
7474
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
75-
if (ASSERT_THROWS_MATCHER.matches(mi) && mi.getArguments().size() == 2) {
75+
if (ASSERT_THROWS_MATCHER.matches(mi)
76+
&& mi.getArguments().size() == 2
77+
&& getCursor().getParentTreeCursor().getValue() instanceof J.Block) {
7678
J executable = mi.getArguments().get(1);
7779
if (executable instanceof J.Lambda) {
7880
executable = ((J.Lambda) executable).withType(THROWING_CALLABLE_TYPE);

src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionTypeTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.InMemoryExecutionContext;
20+
import org.openrewrite.Issue;
2021
import org.openrewrite.java.JavaParser;
2122
import org.openrewrite.test.RecipeSpec;
2223
import org.openrewrite.test.RewriteTest;
@@ -98,4 +99,52 @@ public void throwsWithMemberReference() {
9899
)
99100
);
100101
}
102+
103+
@Test
104+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/pull/331")
105+
void assertThrowsAssignment() {
106+
//language=java
107+
rewriteRun(
108+
java(
109+
"""
110+
import static org.junit.jupiter.api.Assertions.assertThrows;
111+
112+
public class SimpleExpectedExceptionTest {
113+
public void throwsExceptionWithSpecificType() {
114+
NullPointerException npe = assertThrows(NullPointerException.class, () -> {
115+
throw new NullPointerException();
116+
});
117+
}
118+
}
119+
"""
120+
)
121+
);
122+
}
123+
124+
/**
125+
* A degenerate case showing we need to make sure the <code>assertThrows</code> appears
126+
* immediately inside a J.Block.
127+
*/
128+
@Test
129+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/pull/331")
130+
void assertThrowsTernaryAssignment() {
131+
//language=java
132+
rewriteRun(
133+
java(
134+
"""
135+
import static org.junit.jupiter.api.Assertions.assertThrows;
136+
137+
public class SimpleExpectedExceptionTest {
138+
public void throwsExceptionWithSpecificType() {
139+
NullPointerException npe = hashCode() == 42
140+
? new NullPointerException()
141+
: assertThrows(NullPointerException.class, () -> {
142+
throw new NullPointerException();
143+
});
144+
}
145+
}
146+
"""
147+
)
148+
);
149+
}
101150
}

0 commit comments

Comments
 (0)