Skip to content

Commit

Permalink
Support formatting overloads in more AbstractThrowableAssert methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaithanyaGK authored and joel-costigliola committed Sep 21, 2019
1 parent c867a15 commit b9074a6
Show file tree
Hide file tree
Showing 14 changed files with 431 additions and 23 deletions.
132 changes: 131 additions & 1 deletion src/main/java/org/assertj/core/api/AbstractThrowableAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.assertj.core.api;

import static java.lang.String.format;
import static org.assertj.core.error.ShouldNotHaveThrown.shouldNotHaveThrown;

import java.util.IllegalFormatException;
Expand Down Expand Up @@ -91,7 +92,7 @@ public SELF hasMessage(String message) {
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
*/
public SELF hasMessage(String message, Object... parameters) {
return hasMessage(String.format(message, parameters));
return hasMessage(format(message, parameters));
}

/**
Expand Down Expand Up @@ -159,6 +160,15 @@ public SELF hasNoCause() {

/**
* Verifies that the message of the actual {@code Throwable} starts with the given description.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass:
* assertThat(throwableWithMessage).hasMessageStartingWith("wrong amount");
*
* // assertions will fail:
* assertThat(throwableWithMessage).hasMessageStartingWith("right amount"); </code></pre>
*
* @param description the description expected to start the actual {@code Throwable}'s message.
* @return this assertion object.
Expand All @@ -170,6 +180,31 @@ public SELF hasMessageStartingWith(String description) {
return myself;
}

/**
* Verifies that the message of the actual {@code Throwable} starts with the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass:
* assertThat(throwableWithMessage).hasMessageStartingWith("%s amount", "wrong");
*
* // assertions will fail:
* assertThat(throwableWithMessage).hasMessageStartingWith("%s amount", "right"); </code></pre>
*
* @param description the description expected to start the actual {@code Throwable}'s message.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not start with the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
*/
public SELF hasMessageStartingWith(String description, Object... parameters) {
throwables.assertHasMessageStartingWith(info, actual, format(description, parameters));
return myself;
}

/**
* Verifies that the message of the actual {@code Throwable} contains the given description.
* <p>
Expand All @@ -194,6 +229,33 @@ public SELF hasMessageContaining(String description) {
return myself;
}

/**
* Verifies that the message of the actual {@code Throwable} contains the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
* Throwable throwableWithoutMessage = new IllegalArgumentException();
*
* // assertion will pass:
* assertThat(throwableWithMessage).hasMessageContaining("amount %d", 123);
*
* // assertions will fail:
* assertThat(throwableWithoutMessage).hasMessageContaining("amount %d", 123);
* assertThat(throwableWithMessage).hasMessageContaining("%s amount", "right"); </code></pre>
*
* @param description the description expected to be contained in the actual {@code Throwable}'s message.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not contain the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
*/
public SELF hasMessageContaining(String description, Object... parameters) {
throwables.assertHasMessageContaining(info, actual, format(description, parameters));
return myself;
}

/**
* Verifies that the message of the actual {@code Throwable} contains all the given values.
* <p>
Expand Down Expand Up @@ -270,6 +332,15 @@ public SELF hasMessageNotContainingAny(CharSequence... values) {

/**
* Verifies that the stack trace of the actual {@code Throwable} contains the given description.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThat(throwableWithMessage).hasStackTraceContaining("amount 123");
*
* // assertion will fail
* assertThat(throwableWithMessage).hasStackTraceContaining("456");</code></pre>
*
* @param description the description expected to be contained in the actual {@code Throwable}'s stack trace.
* @return this assertion object.
Expand All @@ -281,6 +352,31 @@ public SELF hasStackTraceContaining(String description) {
return myself;
}

/**
* Verifies that the stack trace of the actual {@code Throwable} contains the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThat(throwableWithMessage).hasStackTraceContaining("%s", amount);
*
* // assertion will fail
* assertThat(throwableWithMessage).hasStackTraceContaining("%d", 456);</code></pre>
*
* @param description the description expected to be contained in the actual {@code Throwable}'s stack trace.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the stack trace of the actual {@code Throwable} does not contain the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
*/
public SELF hasStackTraceContaining(String description, Object... parameters) {
throwables.assertHasStackTraceContaining(info, actual, format(description, parameters));
return myself;
}

/**
* Verifies that the message of the actual {@code Throwable} matches the given regular expression.
* <p>
Expand Down Expand Up @@ -334,6 +430,15 @@ public SELF hasMessageFindingMatch(String regex) {

/**
* Verifies that the message of the actual {@code Throwable} ends with the given description.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThat(throwableWithMessage).hasMessageEndingWith("123");
*
* // assertion will fail
* assertThat(throwableWithMessage).hasMessageEndingWith("456");</code></pre>
*
* @param description the description expected to end the actual {@code Throwable}'s message.
* @return this assertion object.
Expand All @@ -345,6 +450,31 @@ public SELF hasMessageEndingWith(String description) {
return myself;
}

/**
* Verifies that the message of the actual {@code Throwable} ends with the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThat(throwableWithMessage).hasMessageEndingWith("%s 123", "amount");
*
* // assertion will fail
* assertThat(throwableWithMessage).hasMessageEndingWith("amount %d", 456);</code></pre>
*
* @param description the description expected to end the actual {@code Throwable}'s message.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not end with the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
*/
public SELF hasMessageEndingWith(String description, Object... parameters) {
throwables.assertHasMessageEndingWith(info, actual, format(description, parameters));
return myself;
}

/**
* Verifies that the cause of the actual {@code Throwable} is an instance of the given type.
* <p>
Expand Down
121 changes: 121 additions & 0 deletions src/main/java/org/assertj/core/api/ThrowableAssertAlternative.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.assertj.core.api;

import java.util.IllegalFormatException;
import org.assertj.core.description.Description;
import org.assertj.core.util.CheckReturnValue;

Expand Down Expand Up @@ -181,6 +182,36 @@ public ThrowableAssertAlternative<T> withMessageStartingWith(String description)
return this;
}

/**
* Verifies that the message of the actual {@code Throwable} starts with the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withMessageStartingWith("%s amount", "wrong");
*
* // assertion will fail
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withMessageStartingWith("%s amount", "right");</code></pre>
*
* @param description the description expected to start the actual {@code Throwable}'s message.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not start with the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
* @see AbstractThrowableAssert#hasMessageStartingWith(String, Object...)
*/
public ThrowableAssertAlternative<T> withMessageStartingWith(String description, Object... parameters) {
delegate.hasMessageStartingWith(description, parameters);
return this;
}

/**
* Verifies that the message of the actual {@code Throwable} contains the given description.
* <p>
Expand Down Expand Up @@ -208,6 +239,36 @@ public ThrowableAssertAlternative<T> withMessageContaining(String description) {
return this;
}

/**
* Verifies that the message of the actual {@code Throwable} contains the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withMessageContaining("%s", amount);
*
* // assertion will fail
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withMessageContaining("%d", 456);</code></pre>
*
* @param description the description expected to be contained in the actual {@code Throwable}'s message.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not contain the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
* @see AbstractThrowableAssert#hasMessageContaining(String, Object...)
*/
public ThrowableAssertAlternative<T> withMessageContaining(String description, Object... parameters) {
delegate.hasMessageContaining(description, parameters);
return this;
}

/**
* Verifies that the message of the actual {@code Throwable} contains all the given values.
* <p>
Expand Down Expand Up @@ -320,6 +381,36 @@ public ThrowableAssertAlternative<T> withStackTraceContaining(String description
return this;
}

/**
* Verifies that the stack trace of the actual {@code Throwable} contains with the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withStackTraceContaining("%s", amount);
*
* // assertion will fail
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withStackTraceContaining("%d", 456);</code></pre>
*
* @param description the description expected to be contained in the actual {@code Throwable}'s stack trace.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the stack trace of the actual {@code Throwable} does not contain the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
* @see AbstractThrowableAssert#hasStackTraceContaining(String, Object...)
*/
public ThrowableAssertAlternative<T> withStackTraceContaining(String description, Object... parameters) {
delegate.hasStackTraceContaining(description, parameters);
return this;
}

/**
* Verifies that the message of the actual {@code Throwable} matches with the given regular expression.
* <p>
Expand Down Expand Up @@ -375,6 +466,36 @@ public ThrowableAssertAlternative<T> withMessageEndingWith(String description) {
return this;
}

/**
* Verifies that the message of the actual {@code Throwable} ends with the given description, after being formatted using
* the {@link String#format} method.
* <p>
* Examples:
* <pre><code class='java'> Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
*
* // assertion will pass
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withMessageEndingWith("%d", 123);
*
* // assertion will fail
* assertThatExceptionOfType(Throwable.class)
* .isThrownBy(() -&gt; {throw illegalArgumentException;})
* .withMessageEndingWith("%d", 456);</code></pre>
*
* @param description the description expected to end the actual {@code Throwable}'s message.
* @param parameters argument referenced by the format specifiers in the format string
* @return this assertion object.
* @throws AssertionError if the actual {@code Throwable} is {@code null}.
* @throws AssertionError if the message of the actual {@code Throwable} does not end with the given description.
* @throws IllegalFormatException if the message contains an illegal syntax according to {@link String#format(String, Object...)}.
* @see AbstractThrowableAssert#hasMessageEndingWith(String, Object...)
*/
public ThrowableAssertAlternative<T> withMessageEndingWith(String description, Object... parameters) {
delegate.hasMessageEndingWith(description, parameters);
return this;
}

/**
* Verifies that the cause of the actual {@code Throwable} is an instance of the given type.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void error_message_contains_stacktrace() {

// Then
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatCode(boom).doesNotThrowAnyException())
.withMessageContaining("java.lang.Exception: boom")
.withMessageContaining("java.lang.Exception: %s", "boom")
.withMessageContaining("at org.assertj.core.api.Assertions_assertThatCode_Test.error_message_contains_stacktrace");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void should_fail_if_completable_future_has_failed() {
assertThatThrownBy(() -> assertThat(future).hasNotFailed()).isInstanceOf(AssertionError.class)
.hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith(format("to not have failed.%n%s", WARNING));
.hasMessageEndingWith("to not have failed.%n%s", WARNING);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void should_fail_if_completable_future_has_failed() {
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith(format("to be completed.%n%s", WARNING));
.hasMessageEndingWith("to be completed.%n%s", WARNING);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void should_fail_if_completable_future_has_failed() {
assertThat(throwable).isInstanceOf(AssertionError.class)
.hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith(format("to be completed.%n%s", WARNING));
.hasMessageEndingWith("to be completed.%n%s", WARNING);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void should_fail_if_completable_future_has_failed() {
assertThatThrownBy(() -> assertThat(future).isCompleted()).isInstanceOf(AssertionError.class)
.hasMessageStartingWith(format("%nExpecting%n <CompletableFuture[Failed: java.lang.RuntimeException]%n"))
.hasMessageContaining("Caused by: java.lang.RuntimeException")
.hasMessageEndingWith(format("to be completed.%n%s", WARNING));
.hasMessageEndingWith("to be completed.%n%s", WARNING);
}

@Test
Expand Down
Loading

0 comments on commit b9074a6

Please sign in to comment.