Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package checks.tests;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import rx.Observable;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertEquals;

public class TooManyAssertionsCheckCustom2 {
Expand Down Expand Up @@ -37,6 +39,35 @@ void test3() { // Noncompliant {{Refactor this method to reduce the number of as
// ^^^^^^^^^^^^^^<
}

@Test
void test4() { // Compliant, chained assertions only count as a single instance
var trueCondition = true;
var falseCondition = false;

assertThat(trueCondition).isTrue().as("true").isTrue().describedAs("isTrue").isTrue();
assertThat(falseCondition).isFalse().as("false").isFalse().describedAs("isFalse").isFalse();
}

@Test
void test5() { // Compliant, chained assertions only count as a single instance
var myObject = new Object();

Assertions.assertThat(myObject).as("someObject").isNotNull().withFailMessage("fail").isNotNull().overridingErrorMessage("error").isInstanceOf(Object.class);
}

@Test
void test6() { // Noncompliant {{Refactor this method to reduce the number of assertions from 3 to less than 2.}}
// ^^^^^
var myObject = new Object();

assertThat(myObject).as("description").isNotNull().describedAs("description").isNotNull();
// ^^^^^^^^^^^^^^^^^^^^<
assertThat(myObject).describedAs("description").isNotNull();
// ^^^^^^^^^^^^^^^^^^^^<
assertThat(myObject).withFailMessage("failure").describedAs("someObject").isNotNull();
// ^^^^^^^^^^^^^^^^^^^^<
}

void customAssert() {
assertEquals(2, f(2));
assertEquals(3, f(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ private class AssertionsCounterVisitor extends BaseTreeVisitor {
@Override
public void visitMethodInvocation(MethodInvocationTree mit) {
super.visitMethodInvocation(mit);
if (isAssertion(methodName(mit), mit.methodSymbol())) {
ExpressionTree methodSelect = mit.methodSelect();
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
ExpressionTree expression = ((MemberSelectExpressionTree) methodSelect).expression();
if (assertions.contains(expression) || chainedAssertions.contains(expression)) {
chainedAssertions.add(mit);
return;
}
ExpressionTree methodSelect = mit.methodSelect();
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
ExpressionTree expression = ((MemberSelectExpressionTree) methodSelect).expression();
if (assertions.contains(expression) || chainedAssertions.contains(expression)) {
chainedAssertions.add(mit);
return;
}
}
if (isAssertion(methodName(mit), mit.methodSymbol())) {
assertions.add(mit);
}
}
Expand Down
Loading