Skip to content

Commit

Permalink
Add soft assertions wasSuccess tests
Browse files Browse the repository at this point in the history
Separate them from other soft assertions tests
  • Loading branch information
joel-costigliola committed Sep 4, 2020
1 parent 79a5142 commit 91db723
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 64 deletions.
32 changes: 0 additions & 32 deletions src/test/java/org/assertj/core/api/BDDSoftAssertionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,34 +162,6 @@ void all_assertions_should_pass() {
softly.assertAll();
}

@Test
void should_return_success_of_last_assertion() {
softly.then(true).isFalse();
softly.then(true).isEqualTo(true);
assertThat(softly.wasSuccess()).isTrue();
}

@Test
void should_return_success_of_last_assertion_with_nested_calls() {
softly.then(true).isFalse();
softly.then(true).isTrue(); // isTrue() calls isEqualTo(true)
assertThat(softly.wasSuccess()).isTrue();
}

@Test
void should_return_failure_of_last_assertion() {
softly.then(true).isTrue();
softly.then(true).isEqualTo(false);
assertThat(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_of_last_assertion_with_nested_calls() {
softly.then(true).isTrue();
softly.then(true).isFalse(); // isFalse() calls isEqualTo(false)
assertThat(softly.wasSuccess()).isFalse();
}

@SuppressWarnings("unchecked")
@Test
void should_be_able_to_catch_exceptions_thrown_by_map_assertions() {
Expand Down Expand Up @@ -854,7 +826,6 @@ void should_return_failure_after_fail() {
// WHEN
softly.fail(failureMessage);
// THEN
assertThat(softly.wasSuccess()).isFalse();
assertThat(softly.errorsCollected()).hasSize(1);
assertThat(softly.errorsCollected().get(0)).hasMessageStartingWith(failureMessage);
}
Expand All @@ -866,7 +837,6 @@ void should_return_failure_after_fail_with_parameters() {
// WHEN
softly.fail(failureMessage, "here", "here");
// THEN
assertThat(softly.wasSuccess()).isFalse();
assertThat(softly.errorsCollected()).hasSize(1);
assertThat(softly.errorsCollected().get(0)).hasMessageStartingWith("Should not reach here or here");
}
Expand All @@ -879,7 +849,6 @@ void should_return_failure_after_fail_with_throwable() {
// WHEN
softly.fail(failureMessage, realCause);
// THEN
assertThat(softly.wasSuccess()).isFalse();
List<Throwable> errorsCollected = softly.errorsCollected();
assertThat(errorsCollected).hasSize(1);
assertThat(errorsCollected.get(0)).hasMessageStartingWith(failureMessage);
Expand All @@ -891,7 +860,6 @@ void should_return_failure_after_shouldHaveThrown() {
// WHEN
softly.shouldHaveThrown(IllegalArgumentException.class);
// THEN
assertThat(softly.wasSuccess()).isFalse();
List<Throwable> errorsCollected = softly.errorsCollected();
assertThat(errorsCollected).hasSize(1);
assertThat(errorsCollected.get(0)).hasMessageStartingWith("IllegalArgumentException should have been thrown");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2020 the original author or authors.
*/
package org.assertj.core.api;

import static org.assertj.core.api.BDDAssertions.then;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("BDD Soft assertions wasSuccess")
class BDDSoftAssertions_wasSuccess_Test extends BaseAssertionsTest {

private BDDSoftAssertions softly;

@BeforeEach
void setup() {
Assertions.setRemoveAssertJRelatedElementsFromStackTrace(false);
softly = new BDDSoftAssertions();
}

@Test
void should_return_success_of_last_assertion() {
softly.then(true).isFalse();
softly.then(true).isEqualTo(true);
then(softly.wasSuccess()).isTrue();
}

@Test
void should_return_success_of_last_assertion_with_nested_calls() {
softly.then(true).isFalse();
softly.then(true).isTrue(); // isTrue() calls isEqualTo(true)
then(softly.wasSuccess()).isTrue();
}

@Test
void should_return_failure_of_last_assertion() {
softly.then(true).isTrue();
softly.then(true).isEqualTo(false);
then(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_of_last_assertion_with_multilple_nested_calls() {
softly.then(true).isTrue();
softly.then(true).isFalse(); // isFalse() calls isEqualTo(false)
then(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_of_last_assertion_with_nested_calls() {
// scenario to avoid:
// -- softly.then(true).isFalse()
// ----- proxied isFalse() -> calls isEqualTo(false) which is proxied
// ------- proxied isEqualTo(false) : catch AssertionError => wasSuccess = false, back to outer call
// ---- proxied isFalse() : no AssertionError caught => last result success = true
softly.then(true).isFalse();
then(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_after_fail() {
// GIVEN
String failureMessage = "Should not reach here";
// WHEN
softly.fail(failureMessage);
// THEN
then(softly.wasSuccess()).isFalse();
then(softly.errorsCollected()).hasSize(1);
then(softly.errorsCollected().get(0)).hasMessageStartingWith(failureMessage);
}

@Test
void should_return_failure_after_fail_with_parameters() {
// GIVEN
String failureMessage = "Should not reach %s or %s";
// WHEN
softly.fail(failureMessage, "here", "here");
// THEN
then(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_after_fail_with_throwable() {
// GIVEN
String failureMessage = "Should not reach here";
IllegalStateException realCause = new IllegalStateException();
// WHEN
softly.fail(failureMessage, realCause);
// THEN
then(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_after_shouldHaveThrown() {
// WHEN
softly.shouldHaveThrown(IllegalArgumentException.class);
// THEN
then(softly.wasSuccess()).isFalse();
}
}
32 changes: 0 additions & 32 deletions src/test/java/org/assertj/core/api/SoftAssertionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,6 @@ void all_assertions_should_pass() {
softly.assertAll();
}

@Test
void should_return_success_of_last_assertion() {
softly.assertThat(true).isFalse();
softly.assertThat(true).isEqualTo(true);
assertThat(softly.wasSuccess()).isTrue();
}

@Test
void should_return_success_of_last_assertion_with_nested_calls() {
softly.assertThat(true).isFalse();
softly.assertThat(true).isTrue(); // isTrue() calls isEqualTo(true)
assertThat(softly.wasSuccess()).isTrue();
}

@Test
void should_return_failure_of_last_assertion() {
softly.assertThat(true).isTrue();
softly.assertThat(true).isEqualTo(false);
assertThat(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_of_last_assertion_with_nested_calls() {
softly.assertThat(true).isTrue();
softly.assertThat(true).isFalse(); // isFalse() calls isEqualTo(false)
assertThat(softly.wasSuccess()).isFalse();
}

@SuppressWarnings("unchecked")
@Test
void should_be_able_to_catch_exceptions_thrown_by_map_assertions() {
Expand Down Expand Up @@ -1026,7 +998,6 @@ void should_return_failure_after_fail() {
// WHEN
softly.fail(failureMessage);
// THEN
assertThat(softly.wasSuccess()).isFalse();
assertThat(softly.errorsCollected()).hasSize(1);
assertThat(softly.errorsCollected().get(0)).hasMessageStartingWith(failureMessage);
}
Expand All @@ -1038,7 +1009,6 @@ void should_return_failure_after_fail_with_parameters() {
// WHEN
softly.fail(failureMessage, "here", "here");
// THEN
assertThat(softly.wasSuccess()).isFalse();
assertThat(softly.errorsCollected()).hasSize(1);
assertThat(softly.errorsCollected().get(0)).hasMessageStartingWith("Should not reach here or here");
}
Expand All @@ -1051,7 +1021,6 @@ void should_return_failure_after_fail_with_throwable() {
// WHEN
softly.fail(failureMessage, realCause);
// THEN
assertThat(softly.wasSuccess()).isFalse();
List<Throwable> errorsCollected = softly.errorsCollected();
assertThat(errorsCollected).hasSize(1);
assertThat(errorsCollected.get(0)).hasMessageStartingWith(failureMessage);
Expand All @@ -1063,7 +1032,6 @@ void should_return_failure_after_shouldHaveThrown() {
// WHEN
softly.shouldHaveThrown(IllegalArgumentException.class);
// THEN
assertThat(softly.wasSuccess()).isFalse();
List<Throwable> errorsCollected = softly.errorsCollected();
assertThat(errorsCollected).hasSize(1);
assertThat(errorsCollected.get(0)).hasMessageStartingWith("IllegalArgumentException should have been thrown");
Expand Down
111 changes: 111 additions & 0 deletions src/test/java/org/assertj/core/api/SoftAssertions_wasSuccess_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2020 the original author or authors.
*/
package org.assertj.core.api;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("Soft assertions wasSuccess")
class SoftAssertions_wasSuccess_Test extends BaseAssertionsTest {

private SoftAssertions softly;

@BeforeEach
void setup() {
Assertions.setRemoveAssertJRelatedElementsFromStackTrace(false);
softly = new SoftAssertions();
}

@Test
void should_return_success_of_last_assertion() {
softly.assertThat(true).isFalse();
softly.assertThat(true).isEqualTo(true);
assertThat(softly.wasSuccess()).isTrue();
}

@Test
void should_return_success_of_last_assertion_with_nested_calls() {
softly.assertThat(true).isFalse();
softly.assertThat(true).isTrue(); // isTrue() calls isEqualTo(true)
assertThat(softly.wasSuccess()).isTrue();
}

@Test
void should_return_failure_of_last_assertion() {
softly.assertThat(true).isTrue();
softly.assertThat(true).isEqualTo(false);
assertThat(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_of_last_assertion_with_multilple_nested_calls() {
softly.assertThat(true).isTrue();
softly.assertThat(true).isFalse(); // isFalse() calls isEqualTo(false)
assertThat(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_of_last_assertion_with_nested_calls() {
// scenario to avoid:
// -- softly.assertThat(true).isFalse()
// ----- proxied isFalse() -> calls isEqualTo(false) which is proxied
// ------- proxied isEqualTo(false) : catch AssertionError => wasSuccess = false, back to outer call
// ---- proxied isFalse() : no AssertionError caught => last result success = true
softly.assertThat(true).isFalse();
assertThat(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_after_fail() {
// GIVEN
String failureMessage = "Should not reach here";
// WHEN
softly.fail(failureMessage);
// THEN
assertThat(softly.wasSuccess()).isFalse();
assertThat(softly.errorsCollected()).hasSize(1);
assertThat(softly.errorsCollected().get(0)).hasMessageStartingWith(failureMessage);
}

@Test
void should_return_failure_after_fail_with_parameters() {
// GIVEN
String failureMessage = "Should not reach %s or %s";
// WHEN
softly.fail(failureMessage, "here", "here");
// THEN
assertThat(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_after_fail_with_throwable() {
// GIVEN
String failureMessage = "Should not reach here";
IllegalStateException realCause = new IllegalStateException();
// WHEN
softly.fail(failureMessage, realCause);
// THEN
assertThat(softly.wasSuccess()).isFalse();
}

@Test
void should_return_failure_after_shouldHaveThrown() {
// WHEN
softly.shouldHaveThrown(IllegalArgumentException.class);
// THEN
assertThat(softly.wasSuccess()).isFalse();
}
}

0 comments on commit 91db723

Please sign in to comment.