Skip to content

Commit

Permalink
Convert tests to use ExpectedException and fix bug in ExpectedExcepti…
Browse files Browse the repository at this point in the history
…on#expectMessage (#833)

* Convert tests to use ExpectedException.

This fixes some tests with would have passed when the expected exception was not thrown.

* fix a bug in ExpectedException#expectMessage

compare expected message to the actual message (instead of checking if the actual message contains the expected message)

* remove unused method ExpectedException#expect(Throwable)

* use ExpectedException#expectIllegalArgumentException

* Use String.format on all string arguments of all methods of ExpectedException.

Adjust all usages of ExpectedException accordingly.

* Add ExpectedException#expectWithCause methods and updates test to use these methods.
  • Loading branch information
PascalSchumacher authored and joel-costigliola committed Dec 22, 2016
1 parent a1a6472 commit ec03f88
Show file tree
Hide file tree
Showing 141 changed files with 685 additions and 1,412 deletions.
Expand Up @@ -47,7 +47,7 @@ public void should_pass_list_asserts_on_list_strings_with_asList() {
public void should_fail_list_asserts_on_non_list_objects_even_with_asList() {
Object nonList = new Object();

thrown.expectAssertionError("an instance of:%n <java.util.List>%nbut was instance of:%n <java.lang.Object>");
thrown.expectAssertionErrorWithMessageContaining("an instance of:%n <java.util.List>%nbut was instance of:%n <java.lang.Object>");
assertThat(nonList).asList().isSorted();
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ public void should_pass_string_asserts_on_string_objects_with_asString() {
public void should_fail_string_asserts_on_non_string_objects_even_with_asString() {
Object nonString = new Object();

thrown.expectAssertionError("an instance of:%n <java.lang.String>%nbut was instance of:%n <java.lang.Object>");
thrown.expectAssertionErrorWithMessageContaining("an instance of:%n <java.lang.String>%nbut was instance of:%n <java.lang.Object>");
assertThat(nonString).asString().contains("hello");
}

Expand Down
Expand Up @@ -51,12 +51,12 @@ public void should_assert_bytes_in_hexadecimal() {

@Test
public void should_assert_bytes_contains_in_hexadecimal() {
thrown.expectMessage("Expecting:%n" +
thrown.expectMessage("%nExpecting:%n" +
" <[0x02, 0x03]>%n" +
"to contain:%n" +
" <[0x01]>%n" +
"but could not find:%n" +
" <[0x01]>");
" <[0x01]>%n");
assertThat(new byte[]{2, 3}).inHexadecimal().contains(new byte[]{1});
}

Expand Down
Expand Up @@ -15,13 +15,18 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Fail.shouldHaveThrown;
import static org.assertj.core.test.ExpectedException.none;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.test.ExpectedException;
import org.junit.Rule;
import org.junit.Test;

public class Assertions_assertThat_with_Throwable_Test {

@Rule
public ExpectedException thrown = none();

@Test
public void should_build_ThrowableAssert_with_runtime_exception_thrown() {
assertThatThrownBy(new ThrowingCallable() {
Expand All @@ -46,20 +51,12 @@ public void call() throws Throwable {

@Test
public void should_fail_if_no_throwable_was_thrown() {
try {
assertThatThrownBy(notRaisingException()).hasMessage("yo");
} catch (AssertionError e) {
assertThat(e).hasMessage("Expecting code to raise a throwable.");
return;
}
shouldHaveThrown(AssertionError.class);
thrown.expectAssertionError("Expecting code to raise a throwable.");
assertThatThrownBy(notRaisingException()).hasMessage("yo");
}

@Test
public void can_capture_exception_and_then_assert_following_AAA_or_BDD_style() {
// given
// some preconditions

// when
Throwable boom = catchThrowable(raisingException("boom!!!!"));

Expand All @@ -70,16 +67,11 @@ public void can_capture_exception_and_then_assert_following_AAA_or_BDD_style() {

@Test
public void fail_with_good_message_when_assertion_is_failing() {
try {
assertThatThrownBy(raisingException("boom")).hasMessage("yo");
} catch (AssertionError ae) {
assertThat(ae).hasMessageContaining("Expecting message:")
.hasMessageContaining("<\"yo\">")
.hasMessageContaining("but was:")
.hasMessageContaining("<\"boom\">");
return;
}
shouldHaveThrown(AssertionError.class);
thrown.expectAssertionErrorWithMessageContaining("Expecting message:",
"<\"yo\">",
"but was:",
"<\"boom\">");
assertThatThrownBy(raisingException("boom")).hasMessage("yo");
}

private ThrowingCallable notRaisingException() {
Expand Down
11 changes: 2 additions & 9 deletions src/test/java/org/assertj/core/api/Assertions_fail_Test.java
Expand Up @@ -12,8 +12,6 @@
*/
package org.assertj.core.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.test.ExpectedException.none;

import org.assertj.core.test.ExpectedException;
Expand Down Expand Up @@ -41,12 +39,7 @@ public void should_include_message_with_parameters_when_failing() {
public void should_include_message_with_cause_when_failing() {
String message = "Some Throwable";
Throwable cause = new Throwable();
try {
Assertions.fail(message, cause);
fail("AssertionError should have been thrown");
} catch (AssertionError e) {
assertThat(message).isEqualTo(e.getMessage());
assertThat(cause).isSameAs(e.getCause());
}
thrown.expectWithCause(AssertionError.class, message, cause);
Assertions.fail(message, cause);
}
}
Expand Up @@ -13,9 +13,12 @@
package org.assertj.core.api;

import static java.util.Collections.emptyList;
import static org.assertj.core.test.ExpectedException.none;
import static org.mockito.Mockito.mock;

import org.assertj.core.internal.Iterables;
import org.assertj.core.test.ExpectedException;
import org.junit.Rule;


/**
Expand All @@ -25,6 +28,9 @@
*/
public abstract class IterableAssertBaseTest extends BaseTestTemplate<ConcreteIterableAssert<Object>, Iterable<Object>>{

@Rule
public ExpectedException thrown = none();

protected Iterables iterables;

@Override
Expand Down
Expand Up @@ -12,8 +12,6 @@
*/
package org.assertj.core.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.test.ExpectedException.none;

import org.assertj.core.test.ExpectedException;
Expand Down Expand Up @@ -41,12 +39,7 @@ public void should_include_message_with_parameters_when_failing() {
public void should_include_message_with_cause_when_failing() {
String message = "Some Throwable";
Throwable cause = new Throwable();
try {
Java6Assertions.fail(message, cause);
fail("AssertionError should have been thrown");
} catch (AssertionError e) {
assertThat(message).isEqualTo(e.getMessage());
assertThat(cause).isSameAs(e.getCause());
}
thrown.expectWithCause(AssertionError.class, message, cause);
Java6Assertions.fail(message, cause);
}
}
Expand Up @@ -12,10 +12,13 @@
*/
package org.assertj.core.api;

import static org.assertj.core.test.ExpectedException.none;
import static org.assertj.core.test.ObjectArrays.emptyArray;
import static org.mockito.Mockito.mock;

import org.assertj.core.internal.ObjectArrays;
import org.assertj.core.test.ExpectedException;
import org.junit.Rule;


/**
Expand All @@ -25,6 +28,9 @@
*/
public abstract class ObjectArrayAssertBaseTest extends BaseTestTemplate<ObjectArrayAssert<Object>, Object[]> {

@Rule
public ExpectedException thrown = none();

protected ObjectArrays arrays;

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/assertj/core/api/ObjectAssertBaseTest.java
Expand Up @@ -12,7 +12,11 @@
*/
package org.assertj.core.api;

import static org.assertj.core.test.ExpectedException.none;

import org.assertj.core.test.ExpectedException;
import org.assertj.core.test.Jedi;
import org.junit.Rule;


/**
Expand All @@ -22,6 +26,9 @@
*/
public abstract class ObjectAssertBaseTest extends BaseTestTemplate<ObjectAssert<Jedi>, Jedi> {

@Rule
public ExpectedException thrown = none();

@Override
protected ObjectAssert<Jedi> create_assertions() {
return new ObjectAssert<>(new Jedi("Yoda", "Green"));
Expand Down
Expand Up @@ -37,7 +37,7 @@ public class AbstractAssert_equal_hashCode_Test {
@Test
@SuppressWarnings("deprecation")
public void should_fail_because_not_supported_operation() {
thrown.expectUnsupportedOperationException("'equals' is not supported...maybe you intended to call 'isEqualTo");
thrown.expectUnsupportedOperationException("'equals' is not supported...maybe you intended to call 'isEqualTo'");
assertions.equals("anotherString");
}

Expand Down
Expand Up @@ -12,12 +12,12 @@
*/
package org.assertj.core.api.abstract_;

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

import static org.assertj.core.test.TestFailures.failBecauseExpectedAssertionErrorWasNotThrown;
import static org.assertj.core.test.ExpectedException.none;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.ConcreteAssert;
import org.assertj.core.test.ExpectedException;
import org.junit.Rule;
import org.junit.Test;


Expand All @@ -28,15 +28,13 @@
*/
public class AbstractAssert_get_writable_info_Test {

@Rule
public ExpectedException thrown = none();

@Test
public void should_keep_specific_error_message_and_description_set_by_user() {
try {
new ConcreteAssert(6L).as("user description").checkNull();
} catch (AssertionError e) {
assertThat(e.getMessage()).isEqualTo("[user description] specific error message");
return;
}
failBecauseExpectedAssertionErrorWasNotThrown();
thrown.expectAssertionError("[user description] specific error message");
new ConcreteAssert(6L).as("user description").checkNull();
}

}
Expand Up @@ -13,12 +13,13 @@
package org.assertj.core.api.abstract_;

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

import static org.assertj.core.test.TestFailures.failBecauseExpectedAssertionErrorWasNotThrown;
import static org.assertj.core.test.ExpectedException.none;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.ConcreteAssert;
import org.assertj.core.test.ExpectedException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;


Expand All @@ -29,6 +30,9 @@
*/
public class AbstractAssert_overridingErrorMessage_Test {

@Rule
public ExpectedException thrown = none();

private ConcreteAssert assertions;

@Before
Expand All @@ -43,48 +47,29 @@ public void should_pass_with_error_message_overridden() {

@Test
public void should_fail_with_overridden_error_message() {
try {
assertions.overridingErrorMessage("new error message").isEqualTo(8L);
} catch (AssertionError err) {
assertThat(err.getMessage()).isEqualTo("new error message");
return;
}
failBecauseExpectedAssertionErrorWasNotThrown();
thrown.expectAssertionError("new error message");
assertions.overridingErrorMessage("new error message").isEqualTo(8L);
}

@Test
public void should_fail_with_overridden_error_message_not_interpreted_with_string_format_feature_as_no_args_are_given() {
try {
assertions.overridingErrorMessage("new error message with special character like (%)").isEqualTo(8L);
} catch (AssertionError err) {
assertThat(err.getMessage()).isEqualTo("new error message with special character like (%)");
return;
}
failBecauseExpectedAssertionErrorWasNotThrown();
// % has to be escaped as %% because expectAssertionError used String.format on the message
thrown.expectAssertionError("new error message with special character like (%%)");
assertions.overridingErrorMessage("new error message with special character like (%)").isEqualTo(8L);
}

@Test
public void should_fail_with_overridden_error_message_interpreted_with_string_format_feature() {
try {
long expected = 8L;
assertions.overridingErrorMessage("new error message, expected value was : '%s'", expected).isEqualTo(expected);
} catch (AssertionError err) {
assertThat(err.getMessage()).isEqualTo("new error message, expected value was : '8'");
return;
}
failBecauseExpectedAssertionErrorWasNotThrown();
thrown.expectAssertionError("new error message, expected value was : '8'");
long expected = 8L;
assertions.overridingErrorMessage("new error message, expected value was : '%s'", expected).isEqualTo(expected);
}

@Test
public void should_fail_with_description_and_overridden_error_message_using_string_format_feature() {
try {
long expected = 8L;
assertions.as("test").overridingErrorMessage("new error message, expected value was : '%s'", expected).isEqualTo(expected);
} catch (AssertionError err) {
assertThat(err.getMessage()).isEqualTo("[test] new error message, expected value was : '8'");
return;
}
failBecauseExpectedAssertionErrorWasNotThrown();
thrown.expectAssertionError("[test] new error message, expected value was : '8'");
long expected = 8L;
assertions.as("test").overridingErrorMessage("new error message, expected value was : '%s'", expected).isEqualTo(expected);
}

@Test
Expand Down
Expand Up @@ -66,7 +66,7 @@ public void should_fail_if_actual_is_null() {

@Test
public void should_fail_if_other_is_not_an_array() {
thrown.expectAssertionError("Expecting an array but was:<\"a string\">");
thrown.expectAssertionError("%nExpecting an array but was:<\"a string\">");
assertThat(new byte[]{1, 2}).hasSameSizeAs("a string");
}

Expand Down
Expand Up @@ -55,13 +55,13 @@ public void succeds_if_actual_is_equal_to_end() {

@Test
public void fails_if_actual_is_less_than_start() {
thrown.expectAssertionError("Expecting:%n <'a'>%nto be between:%n ['b', 'c']");
thrown.expectAssertionError("%nExpecting:%n <'a'>%nto be between:%n ['b', 'c']");
assertThat('a').isBetween('b', 'c');
}

@Test
public void fails_if_actual_is_greater_than_end() {
thrown.expectAssertionError("Expecting:%n <'c'>%nto be between:%n ['a', 'b']");
thrown.expectAssertionError("%nExpecting:%n <'c'>%nto be between:%n ['a', 'b']");
assertThat('c').isBetween('a', 'b');
}
}

0 comments on commit ec03f88

Please sign in to comment.