Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndi Bur2018-01-24 10:41:29 +0000
committerAndi Bur2018-01-24 11:40:35 +0000
commit7a3a461bb3d88fe076654c50aa0a1ec22efa0b0a (patch)
tree0ce2a8c9d752fa203e42af6826af0433b50e4851 /org.eclipse.scout.rt.platform.test
parent4b941be1fa471aadb164b2675a2b4ae119db22b1 (diff)
downloadorg.eclipse.scout.rt-7a3a461bb3d88fe076654c50aa0a1ec22efa0b0a.tar.gz
org.eclipse.scout.rt-7a3a461bb3d88fe076654c50aa0a1ec22efa0b0a.tar.xz
org.eclipse.scout.rt-7a3a461bb3d88fe076654c50aa0a1ec22efa0b0a.zip
Add assertThrows to ScoutAssert
Change-Id: Icaf2369fd366ad134652ceedc01f71885b36e536 Reviewed-on: https://git.eclipse.org/r/115951 Tested-by: Hudson CI Reviewed-by: Andi Bur <andi.bur@gmail.com>
Diffstat (limited to 'org.eclipse.scout.rt.platform.test')
-rw-r--r--org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ITestExecutable.java20
-rw-r--r--org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssert.java21
-rw-r--r--org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssertTest.java49
3 files changed, 88 insertions, 2 deletions
diff --git a/org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ITestExecutable.java b/org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ITestExecutable.java
new file mode 100644
index 0000000000..1c349527be
--- /dev/null
+++ b/org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ITestExecutable.java
@@ -0,0 +1,20 @@
+/*******************************************************************************
+ * Copyright (c) 2018 BSI Business Systems Integration AG.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * BSI Business Systems Integration AG - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.scout.rt.testing.platform.util;
+
+/**
+ * Functional interface allowing lambda expressions that are throwing any {@link Throwable}.
+ */
+@FunctionalInterface
+public interface ITestExecutable {
+
+ void execute() throws Throwable; // NOSONAR squid:S1181
+}
diff --git a/org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssert.java b/org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssert.java
index 1ab85c10ad..5d1b0b69c0 100644
--- a/org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssert.java
+++ b/org.eclipse.scout.rt.platform.test/src/main/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssert.java
@@ -135,4 +135,25 @@ public final class ScoutAssert {
List<String> actualLines = IOUtility.readLines(actualFile, charsetName);
assertListEquals(expectedLines, actualLines);
}
+
+ /**
+ * Asserts that the given {@link ITestExecutable} throws an exception of expected type. <br/>
+ * This method was motivated by Junit 5 and could be replaced when upgrading form Junit 4.
+ */
+ public static <T extends Throwable> T assertThrows(Class<T> expectedType, ITestExecutable r) {
+ try {
+ r.execute();
+ }
+ catch (AssertionError e) {
+ throw e;
+ }
+ catch (Throwable t) { // NOSONAR squid:S1181
+ if (expectedType.isInstance(t)) {
+ return expectedType.cast(t);
+ }
+ throw new AssertionError("Expecting [" + expectedType.getName() + "] but a [" + t.getClass().getName() + "] was thrown", t);
+ }
+
+ throw new AssertionError("Expecting [" + expectedType.getName() + "] but nothing was thrown");
+ }
}
diff --git a/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssertTest.java b/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssertTest.java
index 9594a9948a..da24cb08f4 100644
--- a/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssertTest.java
+++ b/org.eclipse.scout.rt.platform.test/src/test/java/org/eclipse/scout/rt/testing/platform/util/ScoutAssertTest.java
@@ -18,11 +18,13 @@ import static org.junit.Assert.fail;
import java.math.BigDecimal;
+import org.eclipse.scout.rt.platform.exception.PlatformError;
+import org.eclipse.scout.rt.platform.exception.VetoException;
import org.junit.Test;
/**
* Tests for {@link ScoutAssert}.
- *
+ *
* @since 3.10.0-M3
*/
public class ScoutAssertTest {
@@ -30,7 +32,7 @@ public class ScoutAssertTest {
/**
* Test for {@link ScoutAssert#assertComparableEquals(Comparable, Comparable)} and
* {@link ScoutAssert#assertComparableEquals(String, Comparable, Comparable)}.
- *
+ *
* @See Bug 420183
*/
@Test
@@ -96,4 +98,47 @@ public class ScoutAssertTest {
}
}
+ @Test
+ public void testAssertThrows() {
+ // runtime exception
+ final VetoException runtimeException = new VetoException("msg");
+ assertSame(runtimeException, ScoutAssert.assertThrows(VetoException.class, () -> raise(runtimeException)));
+ assertSame(runtimeException, ScoutAssert.assertThrows(RuntimeException.class, () -> raise(runtimeException)));
+ assertSame(runtimeException, ScoutAssert.assertThrows(Exception.class, () -> raise(runtimeException)));
+ assertSame(runtimeException, ScoutAssert.assertThrows(Throwable.class, () -> raise(runtimeException)));
+ try {
+ ScoutAssert.assertThrows(Error.class, () -> raise(runtimeException));
+ fail("expecting assertion to fail");
+ }
+ catch (AssertionError expected) {
+ }
+
+ // runtime exception
+ final InterruptedException exception = new InterruptedException("msg");
+ assertSame(exception, ScoutAssert.assertThrows(InterruptedException.class, () -> raise(exception)));
+ assertSame(exception, ScoutAssert.assertThrows(Exception.class, () -> raise(exception)));
+ assertSame(exception, ScoutAssert.assertThrows(Throwable.class, () -> raise(exception)));
+ try {
+ ScoutAssert.assertThrows(Error.class, () -> raise(exception));
+ fail("expecting assertion to fail");
+ }
+ catch (AssertionError expected) {
+ }
+
+ // error
+ final PlatformError error = new PlatformError("msg");
+ assertSame(error, ScoutAssert.assertThrows(PlatformError.class, () -> raise(error)));
+ assertSame(error, ScoutAssert.assertThrows(Error.class, () -> raise(error)));
+ assertSame(error, ScoutAssert.assertThrows(Throwable.class, () -> raise(error)));
+ try {
+ ScoutAssert.assertThrows(Exception.class, () -> raise(error));
+ fail("expecting assertion to fail");
+ }
+ catch (AssertionError expected) {
+ }
+ }
+
+ private void raise(Throwable t) throws Throwable {
+ throw t;
+ }
}

Back to the top