Skip to content

Commit

Permalink
(yegor256#1396) Add messages & use Throws
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoss committed Jul 15, 2020
1 parent ce17481 commit 6b49542
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 55 deletions.
64 changes: 37 additions & 27 deletions src/test/java/org/cactoos/func/IoCheckedFuncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -40,37 +39,48 @@ public final class IoCheckedFuncTest {
@Test
public void rethrowsIoException() {
final IOException exception = new IOException("intended");
try {
new IoCheckedFunc<>(
(Func<Integer, String>) 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();
}

}
69 changes: 41 additions & 28 deletions src/test/java/org/cactoos/func/IoCheckedProcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -39,37 +39,50 @@ public final class IoCheckedProcTest {
@Test
public void rethrowsIoException() {
final IOException exception = new IOException("intended");
try {
new IoCheckedProc<>(
(Proc<Integer>) 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<Integer>) 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<Integer>) 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();
}

}

0 comments on commit 6b49542

Please sign in to comment.