From 6b49542f3e9c92d1c69ae26684c749efd35650d4 Mon Sep 17 00:00:00 2001 From: andreoss Date: Wed, 15 Jul 2020 04:31:08 -0400 Subject: [PATCH] (#1396) Add messages & use `Throws` --- .../org/cactoos/func/IoCheckedFuncTest.java | 64 +++++++++-------- .../org/cactoos/func/IoCheckedProcTest.java | 69 +++++++++++-------- 2 files changed, 78 insertions(+), 55 deletions(-) diff --git a/src/test/java/org/cactoos/func/IoCheckedFuncTest.java b/src/test/java/org/cactoos/func/IoCheckedFuncTest.java index 5c41195604..c86b6ebe97 100644 --- a/src/test/java/org/cactoos/func/IoCheckedFuncTest.java +++ b/src/test/java/org/cactoos/func/IoCheckedFuncTest.java @@ -24,10 +24,9 @@ package org.cactoos.func; import java.io.IOException; -import org.cactoos.Func; -import org.hamcrest.Matchers; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.Throws; /** * Test case for {@link IoCheckedFunc}. @@ -40,37 +39,48 @@ public final class IoCheckedFuncTest { @Test public void rethrowsIoException() { final IOException exception = new IOException("intended"); - try { - new IoCheckedFunc<>( - (Func) i -> { + new Assertion<>( + "Must rethrow original IOException", + () -> new IoCheckedFunc<>( + i -> { throw exception; } - ).apply(1); - } catch (final IOException ex) { - new Assertion<>( - "", - ex, - Matchers.is(exception) - ).affirm(); - } + ).apply(1), + new Throws<>( + exception.getMessage(), + exception.getClass() + ) + ).affirm(); } - @Test(expected = IOException.class) - public void rethrowsCheckedToIoException() throws Exception { - new IoCheckedFunc<>( - i -> { - throw new IOException("intended to fail"); - } - ).apply(1); + @Test + public void rethrowsCheckedToIoException() { + new Assertion<>( + "Must rethrow as IOException", + () -> new IoCheckedFunc<>( + i -> { + throw new Exception("intended to fail"); + } + ).apply(1), + new Throws<>( + IOException.class + ) + ).affirm(); } - @Test(expected = IllegalStateException.class) - public void runtimeExceptionGoesOut() throws IOException { - new IoCheckedFunc<>( - i -> { - throw new IllegalStateException("intended to fail here"); - } - ).apply(1); + @Test + public void runtimeExceptionGoesOut() { + new Assertion<>( + "Must throw runtime exception as is", + () -> new IoCheckedFunc<>( + i -> { + throw new IllegalStateException("intended to fail here"); + } + ).apply(1), + new Throws<>( + IllegalStateException.class + ) + ).affirm(); } } diff --git a/src/test/java/org/cactoos/func/IoCheckedProcTest.java b/src/test/java/org/cactoos/func/IoCheckedProcTest.java index 663c01b389..ee46ba421d 100644 --- a/src/test/java/org/cactoos/func/IoCheckedProcTest.java +++ b/src/test/java/org/cactoos/func/IoCheckedProcTest.java @@ -25,9 +25,9 @@ import java.io.IOException; import org.cactoos.Proc; -import org.hamcrest.Matchers; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.Throws; /** * Test case for {@link IoCheckedProc}. @@ -39,37 +39,50 @@ public final class IoCheckedProcTest { @Test public void rethrowsIoException() { final IOException exception = new IOException("intended"); - try { - new IoCheckedProc<>( - (Proc) i -> { - throw exception; - } - ).exec(1); - } catch (final IOException ex) { - new Assertion<>( - "", - ex, - Matchers.is(exception) - ).affirm(); - } + new Assertion<>( + "Must rethrow original IOException", + () -> { + new IoCheckedProc<>( + (Proc) i -> { + throw exception; + } + ).exec(1); + return null; + }, + new Throws<>(exception.getMessage(), exception.getClass()) + ).affirm(); } - @Test(expected = IOException.class) - public void rethrowsCheckedToIoException() throws Exception { - new IoCheckedProc<>( - i -> { - throw new InterruptedException("intended to fail"); - } - ).exec(1); + @Test + public void rethrowsCheckedToIoException() { + new Assertion<>( + "Must wrap and throw IOException", + () -> { + new IoCheckedProc<>( + (Proc) i -> { + throw new InterruptedException("intended to fail"); + } + ).exec(1); + return null; + }, + new Throws<>(IOException.class) + ).affirm(); } - @Test(expected = IllegalStateException.class) - public void runtimeExceptionGoesOut() throws IOException { - new IoCheckedProc<>( - i -> { - throw new IllegalStateException("intended to fail here"); - } - ).exec(1); + @Test + public void runtimeExceptionGoesOut() { + new Assertion<>( + "Must throw runtime exceptions as is", + () -> { + new IoCheckedProc<>( + i -> { + throw new IllegalStateException("intended to fail here"); + } + ).exec(1); + return null; + }, + new Throws<>(IllegalStateException.class) + ).affirm(); } }