From 9bf5b6572ff9887b10b62a50688304664713483c Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Wed, 24 Apr 2024 10:13:30 +0200 Subject: [PATCH] Use assertThrows method for asserting exceptions --- .../test/java/org/kitodo/utils/GuardTest.java | 69 +++++++------------ 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java b/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java index 237e77ca576..f4a816c34d7 100644 --- a/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java +++ b/Kitodo-API/src/test/java/org/kitodo/utils/GuardTest.java @@ -12,8 +12,8 @@ package org.kitodo.utils; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.File; import java.util.ArrayList; @@ -24,7 +24,6 @@ public class GuardTest { - @Test public void canCastShouldCast() { Object input = "Hello world!"; @@ -41,25 +40,21 @@ public void canCastShouldCastToSuperclass() { @Test public void canCastShouldNotMiscast() { - try { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { Object input = new ArrayList(); Guard.canCast("input", input, String.class); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("java.util.ArrayList 'input' is not a (subclass of) java.lang.String", e.getMessage()); - } + }); + assertEquals("java.util.ArrayList 'input' is not a (subclass of) java.lang.String", e.getMessage()); } @Test public void isInRangeShouldFailBelowLowerBound() { - try { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { long dice = 0; Guard.isInRange("dice", dice, 1, 6); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("'dice' out of range: 0 not in [1..6]", e.getMessage()); - } + }); + assertEquals("'dice' out of range: 0 not in [1..6]", e.getMessage()); } @@ -83,13 +78,11 @@ public void isInRangeShouldNotFailOnUpperBound() { @Test public void isInRangeShouldFailAboveUpperBound() { - try { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { long dice = 7; Guard.isInRange("dice", dice, 1, 6); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("'dice' out of range: 7 not in [1..6]", e.getMessage()); - } + }); + assertEquals("'dice' out of range: 7 not in [1..6]", e.getMessage()); } @Test @@ -100,13 +93,11 @@ public void isNotNullShouldNotFailForInitializedObjekt() { @Test public void isNotNullShouldFailForNullObjekt() { - try { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { Object object = null; Guard.isNotNull("object", object); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("'object' must not be null", e.getMessage()); - } + }); + assertEquals("'object' must not be null", e.getMessage()); } @Test @@ -123,24 +114,20 @@ public void isPositiveDoubleShouldNotFailForPositiveBoundary() { @Test public void isPositiveDoubleShouldFailForZero() { - try { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { double value = 0; Guard.isPositive("value", value); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("'value' out of range: 0.0 not > 0", e.getMessage()); - } + }); + assertEquals("'value' out of range: 0.0 not > 0", e.getMessage()); } @Test public void isPositiveDoubleShouldFailForNegative() { - try { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { double value = -299_792_458; Guard.isPositive("value", value); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("'value' out of range: -2.99792458E8 not > 0", e.getMessage()); - } + }); + assertEquals("'value' out of range: -2.99792458E8 not > 0", e.getMessage()); } @Test @@ -157,23 +144,19 @@ public void isPositiveLongShouldNotFailForPositiveLowerBound() { @Test public void isPositiveLongShouldFailForZero() { - try { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { long value = 0; Guard.isPositive("value", value); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("'value' out of range: 0 not > 0", e.getMessage()); - } + }); + assertEquals("'value' out of range: 0 not > 0", e.getMessage()); } @Test public void isPositiveLongShouldFailForNegative() { - try { - long value = Long.MAX_VALUE + 1; // is negative due to integer overflow + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> { + long value = Long.MAX_VALUE + 1; // negative due to integer overflow Guard.isPositive("value", value); - fail("should have throws IllegalArgumentException"); - } catch (IllegalArgumentException e) { - assertEquals("'value' out of range: -9223372036854775808 not > 0", e.getMessage()); - } + }); + assertEquals("'value' out of range: -9223372036854775808 not > 0", e.getMessage()); } }