From 0f12a81ee029f0917e16c58c4a6e470788696b4a Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Sun, 27 Oct 2024 07:44:37 +0100 Subject: [PATCH] Change: migrate tests to junit5 --- .../exception/ThrowsExceptionEqualTest.java | 277 +++++++++--------- 1 file changed, 139 insertions(+), 138 deletions(-) diff --git a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java index 83f1a301..d54cf772 100644 --- a/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java +++ b/hamcrest/src/test/java/org/hamcrest/exception/ThrowsExceptionEqualTest.java @@ -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 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 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 void sneakyThrow(Throwable throwable) throws T { - throw (T) throwable; - } + @SuppressWarnings("unchecked") + private void sneakyThrow(Throwable throwable) throws T { + throw (T) throwable; } + } } \ No newline at end of file