Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isNotThrownBy() method as opposite to isThrownBy() #3378

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* <p>
* The class itself does not do much, it delegates the work to {@link ThrowableAssertAlternative} after calling {@link #isThrownBy(ThrowingCallable)}.
*
* @param <T> type of throwable to be thrown.
* @param <T> expected type of throwable to assert.
* @see NotThrownAssert
*/
public class ThrowableTypeAssert<T extends Throwable> implements Descriptable<ThrowableTypeAssert<T>> {
Expand All @@ -45,7 +45,7 @@ public ThrowableTypeAssert(final Class<? extends T> throwableType) {
}

/**
* Assert that an exception of type T is thrown by the {@code throwingCallable}
* Assert that an exception of type {@link T} is thrown by the {@code throwingCallable}
* and allow to chain assertions on the thrown exception.
* <p>
* Example:
Expand All @@ -63,6 +63,25 @@ public ThrowableAssertAlternative<T> isThrownBy(final ThrowingCallable throwingC
return buildThrowableTypeAssert(castThrowable).as(description);
}

/**
* Assert that the {@code throwingCallable} does not throw exception or throw an exception which is not type {@link T}.
* <p>
* Example:
* <pre><code class='java'> assertThatExceptionOfType(IllegalArgumentException.class).isNotThrownBy(() -&gt; { throw new IllegalStateException("boom!"); });</code></pre>
*
* @param throwingCallable code will not throw the exception of expected type
*/
public void isNotThrownBy(final ThrowingCallable throwingCallable) {
Throwable throwable = ThrowableAssert.catchThrowable(throwingCallable);
if (throwable != null) {
checkNotThrowableType(throwable);
}
}

protected void checkNotThrowableType(Throwable throwable) {
assertThat(throwable).as(description).hasBeenThrown().isNotInstanceOf(expectedThrowableType);
}

protected void checkThrowableType(Throwable throwable) {
assertThat(throwable).as(description).hasBeenThrown().isInstanceOf(expectedThrowableType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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-2024 the original author or authors.
*/
package org.assertj.core.api.throwable;

import org.junit.jupiter.api.Test;

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

class ThrowableTypeAssert_isNotThrownBy_Test {

@Test
void should_not_fail_if_nothing_is_thrown_by_callable_code() {
assertThatExceptionOfType(Throwable.class).isNotThrownBy(() -> {});
}

@Test
void should_not_fail_if_expected_exception_is_not_thrown_by_callable_code() {
assertThatExceptionOfType(IllegalArgumentException.class).isNotThrownBy(() -> {
throw new IllegalStateException();
});
}

@Test
void should_fail_if_expected_exception_is_thrown_by_callable_code() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> {
assertThatExceptionOfType(IllegalArgumentException.class).isNotThrownBy(() -> {
throw new IllegalArgumentException();
});
}).withMessageContaining("not to be an instance of: java.lang.IllegalArgumentException");
}
}