From 884d273f4207095f881167b3398fc2a55617ee9a Mon Sep 17 00:00:00 2001 From: Allon Mureinik Date: Tue, 2 Oct 2018 06:41:37 +0300 Subject: [PATCH] Update exception tests to JUnit Jupiter Upgrade the tests in the exception package to use JUnit Jupiter as part of the effort to remove the dependency on the Vintage Engine. While most of these changes are drop-in replacements with no functional benefit, there are some non-obvious changes worth mentioning. Unlike org.junit.Test, org.junit.jupiter.api.Test does not have an "expected" argument. Instead, an explicit call to org.junit.jupiter.api.Assertions.assertThrows is used. Another non-obvious change was performed in ContextedRuntimeExceptionTest. Unlike JUnit Vintages's @Before, JUnit Jupiter's @BeforeEach does not apply if a parent's method is annotated with it and the overriding method is not, so an explicit @BeforeEach annotation had to be added to ContexedTuntimeExceptionTest#setUp(). It's also worth noting this is a minimal patch for migrating the package's tests to Jupiter. There are several tests that can be made made more elegant with Jupiter's new features, but that work is left for subsequent patches. --- .../AbstractExceptionContextTest.java | 12 +++--- .../exception/CloneFailedExceptionTest.java | 41 +++++++++++-------- .../exception/ContextedExceptionTest.java | 10 +++-- .../ContextedRuntimeExceptionTest.java | 12 +++--- .../DefaultExceptionContextTest.java | 6 +-- .../lang3/exception/ExceptionUtilsTest.java | 27 ++++++------ 6 files changed, 59 insertions(+), 49 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java b/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java index 8d5de104834..b9808d4e16b 100644 --- a/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/AbstractExceptionContextTest.java @@ -16,12 +16,12 @@ */ package org.apache.commons.lang3.exception; -import org.junit.Test; -import org.junit.Before; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.Serializable; import java.util.Arrays; @@ -51,7 +51,7 @@ public String toString() { } - @Before + @BeforeEach public void setUp() throws Exception { exceptionContext .addContextValue("test1", null) diff --git a/src/test/java/org/apache/commons/lang3/exception/CloneFailedExceptionTest.java b/src/test/java/org/apache/commons/lang3/exception/CloneFailedExceptionTest.java index 6030f89e448..9a2173e3aa0 100644 --- a/src/test/java/org/apache/commons/lang3/exception/CloneFailedExceptionTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/CloneFailedExceptionTest.java @@ -16,48 +16,55 @@ */ package org.apache.commons.lang3.exception; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * JUnit tests for {@link CloneFailedExceptionTest}. */ public class CloneFailedExceptionTest extends AbstractExceptionTest { - @Test(expected = CloneFailedException.class) - public void testThrowingInformativeException() throws Exception { - throw new CloneFailedException(EXCEPTION_MESSAGE, generateCause()); + @Test + public void testThrowingInformativeException() { + assertThrows(CloneFailedException.class, () -> { + throw new CloneFailedException(EXCEPTION_MESSAGE, generateCause()); + }); } - @Test(expected = CloneFailedException.class) - public void testThrowingExceptionWithMessage() throws Exception { - throw new CloneFailedException(EXCEPTION_MESSAGE); + @Test + public void testThrowingExceptionWithMessage() { + assertThrows(CloneFailedException.class, () -> { + throw new CloneFailedException(EXCEPTION_MESSAGE); + }); } - @Test(expected = CloneFailedException.class) - public void testThrowingExceptionWithCause() throws Exception { - throw new CloneFailedException(generateCause()); + @Test + public void testThrowingExceptionWithCause() { + assertThrows(CloneFailedException.class, () -> { + throw new CloneFailedException(generateCause()); + }); } @Test public void testWithCauseAndMessage() throws Exception { final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE, generateCause()); assertNotNull(exception); - assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage()); + assertEquals(EXCEPTION_MESSAGE, exception.getMessage(), WRONG_EXCEPTION_MESSAGE); final Throwable cause = exception.getCause(); assertNotNull(cause); - assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); + assertEquals(CAUSE_MESSAGE, cause.getMessage(), WRONG_CAUSE_MESSAGE); } @Test public void testWithoutCause() throws Exception { final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE); assertNotNull(exception); - assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage()); + assertEquals(EXCEPTION_MESSAGE, exception.getMessage(), WRONG_EXCEPTION_MESSAGE); final Throwable cause = exception.getCause(); assertNull(cause); @@ -71,6 +78,6 @@ public void testWithoutMessage() throws Exception { final Throwable cause = exception.getCause(); assertNotNull(cause); - assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); + assertEquals(CAUSE_MESSAGE, cause.getMessage(), WRONG_CAUSE_MESSAGE); } } diff --git a/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java b/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java index 9df0e75594d..199e09e3ce2 100644 --- a/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java @@ -16,20 +16,22 @@ */ package org.apache.commons.lang3.exception; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Date; import org.apache.commons.lang3.StringUtils; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * JUnit tests for ContextedException. */ public class ContextedExceptionTest extends AbstractExceptionContextTest { + @BeforeEach @Override public void setUp() throws Exception { exceptionContext = new ContextedException(new Exception(TEST_MESSAGE)); diff --git a/src/test/java/org/apache/commons/lang3/exception/ContextedRuntimeExceptionTest.java b/src/test/java/org/apache/commons/lang3/exception/ContextedRuntimeExceptionTest.java index 3a3217576d6..2794b76f3a3 100644 --- a/src/test/java/org/apache/commons/lang3/exception/ContextedRuntimeExceptionTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/ContextedRuntimeExceptionTest.java @@ -16,23 +16,23 @@ */ package org.apache.commons.lang3.exception; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Date; import org.apache.commons.lang3.StringUtils; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * JUnit tests for ContextedRuntimeException. */ public class ContextedRuntimeExceptionTest extends AbstractExceptionContextTest { + @BeforeEach @Override - @Before public void setUp() throws Exception { exceptionContext = new ContextedRuntimeException(new Exception(TEST_MESSAGE)); super.setUp(); diff --git a/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java b/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java index 5a6c292aea5..00aa50ba82a 100644 --- a/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/DefaultExceptionContextTest.java @@ -16,8 +16,8 @@ */ package org.apache.commons.lang3.exception; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * JUnit tests for DefaultExceptionContext. @@ -25,7 +25,7 @@ public class DefaultExceptionContextTest extends AbstractExceptionContextTest { @Override - @Before + @BeforeEach public void setUp() throws Exception { exceptionContext = new DefaultExceptionContext(); super.setUp(); diff --git a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java index 0af68a8fbc6..c6a403e1329 100644 --- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java @@ -16,12 +16,13 @@ */ package org.apache.commons.lang3.exception; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -33,9 +34,9 @@ import java.util.List; import org.apache.commons.lang3.test.NotVisibleExceptionFactory; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; /** * Tests {@link org.apache.commons.lang3.exception.ExceptionUtils}. @@ -52,7 +53,7 @@ public class ExceptionUtilsTest { private Throwable notVisibleException; - @Before + @BeforeEach public void setUp() { withoutCause = createExceptionWithoutCause(); nested = new NestableException(withoutCause); @@ -66,7 +67,7 @@ public void setUp() { } - @After + @AfterEach public void tearDown() throws Exception { withoutCause = null; nested = null; @@ -449,9 +450,9 @@ public void testGetRootCauseStackTrace_Throwable() throws Exception { assertFalse(match); } - @Test(expected = IllegalArgumentException.class) + @Test public void testRemoveCommonFrames_ListList() throws Exception { - ExceptionUtils.removeCommonFrames(null, null); + assertThrows(IllegalArgumentException.class, () -> ExceptionUtils.removeCommonFrames(null, null)); } @Test