Skip to content

Commit

Permalink
#739 junit<4/5>:AssertionMustProvideMessage returned false positives …
Browse files Browse the repository at this point in the history
…for assert methods with a non-void return type. (#740)

Co-authored-by: Moritz Pflügner <[email protected]>
  • Loading branch information
morpfl and Moritz Pflügner authored Dec 20, 2024
1 parent 7573e44 commit 9f3ac59
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
WHERE
assertType.fqn = 'org.junit.Assert'
and (
assertMethod.signature =~ 'void assert.*'
or assertMethod.signature =~ 'void fail.*'
assertMethod.signature CONTAINS ' assert'
or assertMethod.signature CONTAINS ' fail'
)
SET
assertMethod:Junit4:Assert
Expand Down Expand Up @@ -208,7 +208,7 @@
(testType:Type)-[:DECLARES]->(testMethod:Test:Method),
(testMethod)-[invocation:INVOKES*]->(assertMethod:Junit4:Assert:Method)
WHERE NOT (
assertMethod.signature =~ 'void assert.*\\(java.lang.String,.*\\)'
assertMethod.signature =~ '.*assert.*\\(java.lang.String,.*\\)'
or assertMethod.signature = 'void fail(java.lang.String)'
)
RETURN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@
(testType:Type)-[:DECLARES]->(testMethod:Test:Method),
(testMethod)-[invocation:INVOKES]->(assertMethod:Junit5:Assert:Method)
WHERE NOT (
assertMethod.signature =~ 'void assert.*\\(.*java.lang.String\\)'
or assertMethod.signature =~ 'void assert.*\\(.*java.util.function.Supplier\\)'
assertMethod.signature =~ '.*assert.*\\(.*java.lang.String\\)'
or assertMethod.signature =~ '.*assert.*\\(.*java.util.function.Supplier\\)'
or assertMethod.signature = 'java.lang.Object fail(java.lang.String)'
)
RETURN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.buschmais.jqassistant.plugin.junit.test.set.junit5.Assertions4Junit5;

import org.junit.Assert;
import org.junit.function.ThrowingRunnable;
import org.junit.jupiter.api.Test;

import static com.buschmais.jqassistant.core.report.api.model.Result.Status.FAILURE;
Expand Down Expand Up @@ -173,8 +174,9 @@ public void assertMethod() throws Exception {
assertThat(applyConcept("java:AssertMethod").getStatus(), equalTo(SUCCESS));
store.beginTransaction();
List<MethodDescriptor> methods = query("match (m:Assert:Junit4:Method) return m").getColumn("m");
assertThat(methods, containsInAnyOrder(methodDescriptor(Assert.class, "assertTrue", boolean.class),
methodDescriptor(Assert.class, "assertTrue", String.class, boolean.class), methodDescriptor(Assert.class, "fail", String.class)));
assertThat(methods, containsInAnyOrder(methodDescriptor(Assert.class, "assertThrows", String.class, Class.class, ThrowingRunnable.class),
methodDescriptor(Assert.class, "assertTrue", boolean.class), methodDescriptor(Assert.class, "assertTrue", String.class, boolean.class),
methodDescriptor(Assert.class, "fail", String.class)));
store.commitTransaction();
}

Expand Down Expand Up @@ -344,7 +346,7 @@ public void nonJUnit4TestMethod() throws Exception {
.get("TestMethod")
.getValue())
.collect(Collectors.toList());
assertThat(rows.size(), equalTo(12));
assertThat(rows.size(), equalTo(13));
assertThat(rows, containsInAnyOrder(is(methodDescriptor(Assertions4Junit5.class, "assertWithoutMessage")),
is(methodDescriptor(Assertions4Junit5.class, "assertWithMessageSupplier")), is(methodDescriptor(Assertions4Junit5.class, "assertWithMessage")),
is(methodDescriptor(Assertions4Junit5.class, "repeatedTestWithoutAssertion")),
Expand All @@ -354,7 +356,8 @@ public void nonJUnit4TestMethod() throws Exception {
is(methodDescriptor(Assertions4Junit5.class, "testWithDeepNestedAssertion")),
is(methodDescriptor(Assertions4Junit5.class, "testWithDeepAndShallowAssertion")),
is(methodDescriptor(Assertions4Junit5.class, "assertWithNonVoidReturn")),
is(methodDescriptor(Assertions4Junit5.class, "assertInSuperClass"))));
is(methodDescriptor(Assertions4Junit5.class, "assertInSuperClass")),
is(methodDescriptor(Assertions4Junit5.class, "assertWithMessageButNonVoidReturnType"))));
store.commitTransaction();
}

Expand All @@ -378,11 +381,12 @@ public void usageOfJUnit5TestApi() throws Exception {
.get("TestMethod")
.getValue())
.collect(Collectors.toList());
assertThat(rows.size(), equalTo(7));
assertThat(rows.size(), equalTo(8));
assertThat(rows, containsInAnyOrder(is(methodDescriptor(Assertions4Junit5.class, "assertWithoutMessage")),
is(methodDescriptor(Assertions4Junit5.class, "assertWithMessageSupplier")), is(methodDescriptor(Assertions4Junit5.class, "assertWithMessage")),
is(methodDescriptor(Assertions4Junit5.class, "testWithAssertion")), is(methodDescriptor(Assertions4Junit5.class, "testWithNestedAssertion")),
is(methodDescriptor(Assertions4Junit5.class, "assertWithNonVoidReturn")), is(methodDescriptor(Assertions4Junit5.class, "testWithDeepAndShallowAssertion"))));
is(methodDescriptor(Assertions4Junit5.class, "assertWithNonVoidReturn")), is(methodDescriptor(Assertions4Junit5.class, "testWithDeepAndShallowAssertion")),
is(methodDescriptor(Assertions4Junit5.class, "assertWithMessageButNonVoidReturnType"))));
store.commitTransaction();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,12 @@ public void nonJUnit5TestMethod() throws Exception {
.get("TestMethod")
.getValue())
.collect(Collectors.toList());
assertThat(rows.size(), equalTo(6));
assertThat(rows.size(), equalTo(7));
assertThat(rows, containsInAnyOrder(is(methodDescriptor(Assertions4Junit4.class, "assertWithoutMessage")),
is(methodDescriptor(Assertions4Junit4.class, "assertWithMessage")), is(methodDescriptor(Assertions4Junit4.class, "testWithoutAssertion")),
is(methodDescriptor(Assertions4Junit4.class, "testWithAssertion")), is(methodDescriptor(Assertions4Junit4.class, "testWithNestedAssertion")),
is(methodDescriptor(Assertions4Junit4.class, "testWithExpectedRuntimeException"))));
is(methodDescriptor(Assertions4Junit4.class, "testWithExpectedRuntimeException")),
is(methodDescriptor(Assertions4Junit4.class, "assertWithMessageButNonVoidReturnType"))));
store.commitTransaction();
}

Expand All @@ -492,10 +493,11 @@ public void usageOfJUnit4TestApi() throws Exception {
.get("TestMethod")
.getValue())
.collect(Collectors.toList());
assertThat(rows.size(), equalTo(4));
assertThat(rows.size(), equalTo(5));
assertThat(rows, containsInAnyOrder(is(methodDescriptor(Assertions4Junit4.class, "assertWithoutMessage")),
is(methodDescriptor(Assertions4Junit4.class, "assertWithMessage")), is(methodDescriptor(Assertions4Junit4.class, "testWithAssertion")),
is(methodDescriptor(Assertions4Junit4.class, "testWithNestedAssertion"))));
is(methodDescriptor(Assertions4Junit4.class, "testWithNestedAssertion")),
is(methodDescriptor(Assertions4Junit4.class, "assertWithMessageButNonVoidReturnType"))));
store.commitTransaction();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -14,6 +15,11 @@ public void assertWithoutMessage() {
assertTrue(true);
}

@Test
public void assertWithMessageButNonVoidReturnType() {
assertThrows("Condition must be true", NullPointerException.class, null);
}

@Test
public void assertWithMessage() {
assertTrue("Condition must be true", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public void assertWithoutMessage() {
assertTrue(true);
}

@Test
public void assertWithMessageButNonVoidReturnType() {
assertThrows(NullPointerException.class, null, "Condition must be true");
}

@Test
public void assertWithMessageSupplier() {
assertTrue(() -> true, () -> "S");
Expand Down

0 comments on commit 9f3ac59

Please sign in to comment.