Add thenThrow(Exception)

This commit is contained in:
Paul Campbell 2018-07-18 21:57:25 +01:00
parent d985715f7e
commit 752b7864be
5 changed files with 40 additions and 3 deletions

View file

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
0.5.0
-----
* Add `thenThrow(Exception)`
0.4.0 0.4.0
----- -----

View file

@ -123,4 +123,12 @@ public interface Condition {
return where(clause.get()); return where(clause.get());
} }
/**
* Throw then exception if the {@code Condition} is {@code true}.
*
* @param exception the Exception to throw
* @throws Exception the exception
*/
@SuppressWarnings({"illegalthrows", "PMD.SignatureDeclareThrowsException"})
void thenThrow(Exception exception) throws Exception;
} }

View file

@ -53,4 +53,9 @@ final class FalseCondition implements Condition {
response.perform(); response.perform();
} }
@Override
public void thenThrow(final Exception exception) {
// do nothing
}
} }

View file

@ -54,4 +54,9 @@ final class TrueCondition implements Condition {
// do nothing // do nothing
} }
@Override
public void thenThrow(final Exception exception) throws Exception {
throw exception;
}
} }

View file

@ -1,16 +1,16 @@
package net.kemitix.conditional; package net.kemitix.conditional;
import org.assertj.core.api.WithAssertions;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Paul Campbell (pcampbell@kemitix.net). * @author Paul Campbell (pcampbell@kemitix.net).
*/ */
public class ConditionalTest { public class ConditionalTest implements WithAssertions {
private Action thenResponse; private Action thenResponse;
@ -311,4 +311,18 @@ public class ConditionalTest {
assertThatTheThenResponseDidNotRun(); assertThatTheThenResponseDidNotRun();
assertThat(atomicInteger).hasValue(0); assertThat(atomicInteger).hasValue(0);
} }
@Test
public void whereTrue_thenThrowException() {
//given
assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> Condition.where(true)
.thenThrow(new IOException()));
}
@Test
public void whereFalse_thenDoNotThrowException() throws Exception {
Condition.where(false)
.thenThrow(new IOException());
}
} }