Skip to content

Commit

Permalink
Change: migrate tests to junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
ggalmazor committed Oct 27, 2024
1 parent d1b6b8f commit 0f12a81
Showing 1 changed file with 139 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,155 +1,156 @@
package org.hamcrest.exception;

import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.exception.ThrowsException.throwsException;
import static org.hamcrest.exception.ThrowsExceptionWithMessage.withMessage;

public class ThrowsExceptionEqualTest extends AbstractMatcherTest {

private Runnable runnableThrowing(Throwable exception) {
return new ThrowingRunnable(exception);
}

@Override
protected Matcher<?> createMatcher() {
return throwsException(IllegalArgumentException.class);
}

public void testExamples() {
RuntimeException boom = new RuntimeException("boom");
Runnable codeThatThrows = () -> {
throw boom;
};

assertThat(codeThatThrows, throwsException());
assertThat(codeThatThrows, throwsException(boom));
assertThat(codeThatThrows, throwsException(RuntimeException.class));
assertThat(codeThatThrows, throwsException(withMessage("boom")));
assertThat(codeThatThrows, throwsException(withMessage(containsString("oom"))));
assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom"));
assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage("boom")));
assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage(containsString("boom"))));
}

public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() {
assertMatches(
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new IllegalArgumentException("Boom!"))
);

assertDescription(
"a runnable throwing \"java.lang.IllegalArgumentException\" with message \"Boom!\"",
throwsException(new IllegalArgumentException("Boom!"))
);

assertMismatchDescription(
"the runnable threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"",
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new IllegalArgumentException("Bang!"))
);

assertMismatchDescription(
"the runnable threw \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception",
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new NullPointerException("Boom!"))
);

assertMismatchDescription(
"the runnable didn't throw",
throwsException(new IllegalArgumentException("Boom!")),
(Runnable) () -> {
}
);
}

public void testEvaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() {
assertMatches(
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new TestException("Boom!"))
);
}

public void testEvaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() {
assertMatches(
throwsException(withMessage("Boom!")),
runnableThrowing(new TestException("Boom!"))
);

assertDescription(
"a runnable throwing an exception with message \"Boom!\"",
throwsException(withMessage("Boom!"))
);

assertMismatchDescription(
"the runnable threw an exception with message \"Bang!\" instead of \"Boom!\"",
throwsException(withMessage("Boom!")),
runnableThrowing(new IllegalArgumentException("Bang!"))
);
import static org.hamcrest.test.MatcherAssertions.*;

public final class ThrowsExceptionEqualTest {

private Runnable runnableThrowing(Throwable exception) {
return new ThrowingRunnable(exception);
}

@Test
public void examples() {
RuntimeException boom = new RuntimeException("boom");
Runnable codeThatThrows = () -> {
throw boom;
};

assertThat(codeThatThrows, throwsException());
assertThat(codeThatThrows, throwsException(boom));
assertThat(codeThatThrows, throwsException(RuntimeException.class));
assertThat(codeThatThrows, throwsException(withMessage("boom")));
assertThat(codeThatThrows, throwsException(withMessage(containsString("oom"))));
assertThat(codeThatThrows, throwsException(RuntimeException.class, "boom"));
assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage("boom")));
assertThat(codeThatThrows, throwsException(RuntimeException.class, withMessage(containsString("boom"))));
}

@Test
public void evaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage() {
assertMatches(
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new IllegalArgumentException("Boom!"))
);

assertDescription(
"a runnable throwing \"java.lang.IllegalArgumentException\" with message \"Boom!\"",
throwsException(new IllegalArgumentException("Boom!"))
);

assertMismatchDescription(
"the runnable threw \"java.lang.IllegalArgumentException\" with message \"Bang!\" instead of \"Boom!\"",
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new IllegalArgumentException("Bang!"))
);

assertMismatchDescription(
"the runnable threw \"java.lang.NullPointerException\" instead of a \"java.lang.IllegalArgumentException\" exception",
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new NullPointerException("Boom!"))
);

assertMismatchDescription(
"the runnable didn't throw",
throwsException(new IllegalArgumentException("Boom!")),
(Runnable) () -> {
}
);
}

@Test
public void evaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage() {
assertMatches(
throwsException(new IllegalArgumentException("Boom!")),
runnableThrowing(new TestException("Boom!"))
);
}

@Test
public void evaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage() {
assertMatches(
throwsException(withMessage("Boom!")),
runnableThrowing(new TestException("Boom!"))
);

assertDescription(
"a runnable throwing an exception with message \"Boom!\"",
throwsException(withMessage("Boom!"))
);

assertMismatchDescription(
"the runnable threw an exception with message \"Bang!\" instead of \"Boom!\"",
throwsException(withMessage("Boom!")),
runnableThrowing(new IllegalArgumentException("Bang!"))
);
}

@Test
public void evaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() {

assertMatches(
throwsException(withMessage(containsString("bar"))),
runnableThrowing(new TestException("Foo bar baz"))
);

assertDescription(
"a runnable throwing an exception with message a string containing \"bar\"",
throwsException(withMessage(containsString("bar")))
);

assertMismatchDescription(
"the runnable threw an exception with message \"Bang!\" instead of a string containing \"bar\"",
throwsException(withMessage(containsString("bar"))),
runnableThrowing(new IllegalArgumentException("Bang!"))
);
}

@Test
public void evaluatesToTrueIfRunnableThrowsMatchingException() {
assertMatches(
throwsException(instanceOf(TestException.class)),
runnableThrowing(new TestException("Boom!"))
);

assertDescription(
"a runnable throwing an instance of java.lang.NullPointerException",
throwsException(instanceOf(NullPointerException.class))
);

assertMismatchDescription(
"the runnable threw <java.lang.IllegalArgumentException: Bang!> is a java.lang.IllegalArgumentException",
throwsException(instanceOf(NullPointerException.class)),
runnableThrowing(new IllegalArgumentException("Bang!"))
);
}

public static class TestException extends IllegalArgumentException {
public TestException(String message) {
super(message);
}
}

public void testEvaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage() {

assertMatches(
throwsException(withMessage(containsString("bar"))),
runnableThrowing(new TestException("Foo bar baz"))
);
static class ThrowingRunnable implements Runnable {
private final Throwable throwable;

assertDescription(
"a runnable throwing an exception with message a string containing \"bar\"",
throwsException(withMessage(containsString("bar")))
);

assertMismatchDescription(
"the runnable threw an exception with message \"Bang!\" instead of a string containing \"bar\"",
throwsException(withMessage(containsString("bar"))),
runnableThrowing(new IllegalArgumentException("Bang!"))
);
ThrowingRunnable(Throwable throwable) {
this.throwable = throwable;
}

public void testEvaluatesToTrueIfRunnableThrowsMatchingException() {
assertMatches(
throwsException(instanceOf(TestException.class)),
runnableThrowing(new TestException("Boom!"))
);

assertDescription(
"a runnable throwing an instance of java.lang.NullPointerException",
throwsException(instanceOf(NullPointerException.class))
);

assertMismatchDescription(
"the runnable threw <java.lang.IllegalArgumentException: Bang!> is a java.lang.IllegalArgumentException",
throwsException(instanceOf(NullPointerException.class)),
runnableThrowing(new IllegalArgumentException("Bang!"))
);
}

public static class TestException extends IllegalArgumentException {
public TestException(String message) {
super(message);
}
@Override
public void run() {
sneakyThrow(throwable);
}

static class ThrowingRunnable implements Runnable {
private final Throwable throwable;

ThrowingRunnable(Throwable throwable) {
this.throwable = throwable;
}

@Override
public void run() {
sneakyThrow(throwable);
}

@SuppressWarnings("unchecked")
private <T extends Throwable> void sneakyThrow(Throwable throwable) throws T {
throw (T) throwable;
}
@SuppressWarnings("unchecked")
private <T extends Throwable> void sneakyThrow(Throwable throwable) throws T {
throw (T) throwable;
}
}
}

0 comments on commit 0f12a81

Please sign in to comment.