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.
This commit is contained in:
Allon Mureinik 2018-10-02 06:41:37 +03:00
parent 762641dcdb
commit 884d273f42
6 changed files with 59 additions and 49 deletions

View File

@ -16,12 +16,12 @@
*/ */
package org.apache.commons.lang3.exception; package org.apache.commons.lang3.exception;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
@ -51,7 +51,7 @@ public abstract class AbstractExceptionContextTest<T extends ExceptionContext &
} }
@Before @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
exceptionContext exceptionContext
.addContextValue("test1", null) .addContextValue("test1", null)

View File

@ -16,48 +16,55 @@
*/ */
package org.apache.commons.lang3.exception; 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.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** /**
* JUnit tests for {@link CloneFailedExceptionTest}. * JUnit tests for {@link CloneFailedExceptionTest}.
*/ */
public class CloneFailedExceptionTest extends AbstractExceptionTest { public class CloneFailedExceptionTest extends AbstractExceptionTest {
@Test(expected = CloneFailedException.class) @Test
public void testThrowingInformativeException() throws Exception { public void testThrowingInformativeException() {
assertThrows(CloneFailedException.class, () -> {
throw new CloneFailedException(EXCEPTION_MESSAGE, generateCause()); throw new CloneFailedException(EXCEPTION_MESSAGE, generateCause());
});
} }
@Test(expected = CloneFailedException.class) @Test
public void testThrowingExceptionWithMessage() throws Exception { public void testThrowingExceptionWithMessage() {
assertThrows(CloneFailedException.class, () -> {
throw new CloneFailedException(EXCEPTION_MESSAGE); throw new CloneFailedException(EXCEPTION_MESSAGE);
});
} }
@Test(expected = CloneFailedException.class) @Test
public void testThrowingExceptionWithCause() throws Exception { public void testThrowingExceptionWithCause() {
assertThrows(CloneFailedException.class, () -> {
throw new CloneFailedException(generateCause()); throw new CloneFailedException(generateCause());
});
} }
@Test @Test
public void testWithCauseAndMessage() throws Exception { public void testWithCauseAndMessage() throws Exception {
final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE, generateCause()); final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE, generateCause());
assertNotNull(exception); assertNotNull(exception);
assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage()); assertEquals(EXCEPTION_MESSAGE, exception.getMessage(), WRONG_EXCEPTION_MESSAGE);
final Throwable cause = exception.getCause(); final Throwable cause = exception.getCause();
assertNotNull(cause); assertNotNull(cause);
assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); assertEquals(CAUSE_MESSAGE, cause.getMessage(), WRONG_CAUSE_MESSAGE);
} }
@Test @Test
public void testWithoutCause() throws Exception { public void testWithoutCause() throws Exception {
final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE); final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE);
assertNotNull(exception); assertNotNull(exception);
assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage()); assertEquals(EXCEPTION_MESSAGE, exception.getMessage(), WRONG_EXCEPTION_MESSAGE);
final Throwable cause = exception.getCause(); final Throwable cause = exception.getCause();
assertNull(cause); assertNull(cause);
@ -71,6 +78,6 @@ public class CloneFailedExceptionTest extends AbstractExceptionTest {
final Throwable cause = exception.getCause(); final Throwable cause = exception.getCause();
assertNotNull(cause); assertNotNull(cause);
assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); assertEquals(CAUSE_MESSAGE, cause.getMessage(), WRONG_CAUSE_MESSAGE);
} }
} }

View File

@ -16,20 +16,22 @@
*/ */
package org.apache.commons.lang3.exception; package org.apache.commons.lang3.exception;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Date; import java.util.Date;
import org.apache.commons.lang3.StringUtils; 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. * JUnit tests for ContextedException.
*/ */
public class ContextedExceptionTest extends AbstractExceptionContextTest<ContextedException> { public class ContextedExceptionTest extends AbstractExceptionContextTest<ContextedException> {
@BeforeEach
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
exceptionContext = new ContextedException(new Exception(TEST_MESSAGE)); exceptionContext = new ContextedException(new Exception(TEST_MESSAGE));

View File

@ -16,23 +16,23 @@
*/ */
package org.apache.commons.lang3.exception; package org.apache.commons.lang3.exception;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Date; import java.util.Date;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
/** /**
* JUnit tests for ContextedRuntimeException. * JUnit tests for ContextedRuntimeException.
*/ */
public class ContextedRuntimeExceptionTest extends AbstractExceptionContextTest<ContextedRuntimeException> { public class ContextedRuntimeExceptionTest extends AbstractExceptionContextTest<ContextedRuntimeException> {
@BeforeEach
@Override @Override
@Before
public void setUp() throws Exception { public void setUp() throws Exception {
exceptionContext = new ContextedRuntimeException(new Exception(TEST_MESSAGE)); exceptionContext = new ContextedRuntimeException(new Exception(TEST_MESSAGE));
super.setUp(); super.setUp();

View File

@ -16,8 +16,8 @@
*/ */
package org.apache.commons.lang3.exception; package org.apache.commons.lang3.exception;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
/** /**
* JUnit tests for DefaultExceptionContext. * JUnit tests for DefaultExceptionContext.
@ -25,7 +25,7 @@ import org.junit.Test;
public class DefaultExceptionContextTest extends AbstractExceptionContextTest<DefaultExceptionContext> { public class DefaultExceptionContextTest extends AbstractExceptionContextTest<DefaultExceptionContext> {
@Override @Override
@Before @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
exceptionContext = new DefaultExceptionContext(); exceptionContext = new DefaultExceptionContext();
super.setUp(); super.setUp();

View File

@ -16,12 +16,13 @@
*/ */
package org.apache.commons.lang3.exception; package org.apache.commons.lang3.exception;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.Assert.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -33,9 +34,9 @@ import java.lang.reflect.Modifier;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.test.NotVisibleExceptionFactory; import org.apache.commons.lang3.test.NotVisibleExceptionFactory;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
/** /**
* Tests {@link org.apache.commons.lang3.exception.ExceptionUtils}. * Tests {@link org.apache.commons.lang3.exception.ExceptionUtils}.
@ -52,7 +53,7 @@ public class ExceptionUtilsTest {
private Throwable notVisibleException; private Throwable notVisibleException;
@Before @BeforeEach
public void setUp() { public void setUp() {
withoutCause = createExceptionWithoutCause(); withoutCause = createExceptionWithoutCause();
nested = new NestableException(withoutCause); nested = new NestableException(withoutCause);
@ -66,7 +67,7 @@ public class ExceptionUtilsTest {
} }
@After @AfterEach
public void tearDown() throws Exception { public void tearDown() throws Exception {
withoutCause = null; withoutCause = null;
nested = null; nested = null;
@ -449,9 +450,9 @@ public class ExceptionUtilsTest {
assertFalse(match); assertFalse(match);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testRemoveCommonFrames_ListList() throws Exception { public void testRemoveCommonFrames_ListList() throws Exception {
ExceptionUtils.removeCommonFrames(null, null); assertThrows(IllegalArgumentException.class, () -> ExceptionUtils.removeCommonFrames(null, null));
} }
@Test @Test