From 233b225b4fcb628e0a15c18f94941e2bdd5c6af2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 11 May 2019 18:03:42 +0200 Subject: [PATCH] Remove JUnit dependency from HeaderResultMatchers Prior to this commit, HeaderResultMatchers had a direct dependency on org.junit.Assert which forces JUnit 4 to be present on the classpath. This commit fixes this by introducing assertNotNull() in org.springframework.test.util.AssertionErrors and delegating to that instead. Fixes gh-22932 --- .../test/util/AssertionErrors.java | 38 ++++++++++++------- .../servlet/result/HeaderResultMatchers.java | 4 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java b/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java index 941a993d966..1c47743aa29 100644 --- a/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java +++ b/spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,24 +20,25 @@ import org.springframework.lang.Nullable; import org.springframework.util.ObjectUtils; /** - * JUnit independent assertion class. + * Test assertions that are independent of any third-party assertion library. * * @author Lukas Krecan * @author Arjen Poutsma + * @author Sam Brannen * @since 3.2 */ public abstract class AssertionErrors { /** - * Fails a test with the given message. - * @param message describes the reason for the failure + * Fail a test with the given message. + * @param message a message that describes the reason for the failure */ public static void fail(String message) { throw new AssertionError(message); } /** - * Fails a test with the given message passing along expected and actual + * Fail a test with the given message passing along expected and actual * values to be added to the message. *

For example given: *

@@ -47,9 +48,9 @@ public abstract class AssertionErrors {
 	 * 
 	 * Response header [Accept] expected:<application/json> but was:<text/plain>
 	 * 
- * @param message describes the value that failed the match - * @param expected expected value - * @param actual actual value + * @param message a message describes the value that failed the match + * @param expected the expected value + * @param actual the actual value */ public static void fail(String message, @Nullable Object expected, @Nullable Object actual) { throw new AssertionError(message + " expected:<" + expected + "> but was:<" + actual + ">"); @@ -57,8 +58,8 @@ public abstract class AssertionErrors { /** * Assert the given condition is {@code true} and raise an - * {@link AssertionError} if it is not. - * @param message the message + * {@link AssertionError} otherwise. + * @param message a message that describes the reason for the failure * @param condition the condition to test for */ public static void assertTrue(String message, boolean condition) { @@ -68,12 +69,23 @@ public abstract class AssertionErrors { } /** - * Assert two objects are equal and raise an {@link AssertionError} if not. + * Assert that the given object is not {@code null} and raise an + * {@link AssertionError} otherwise. + * @param message a message that describes the reason for the failure + * @param object the object to check + * @since 5.1 + */ + public static void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + + /** + * Assert two objects are equal and raise an {@link AssertionError} otherwise. *

For example: *

 	 * assertEquals("Response header [" + name + "]", expected, actual);
 	 * 
- * @param message describes the value being checked + * @param message a message that describes the reason for the failure * @param expected the expected value * @param actual the actual value */ @@ -89,7 +101,7 @@ public abstract class AssertionErrors { *
 	 * assertNotEquals("Response header [" + name + "]", expected, actual);
 	 * 
- * @param message describes the value being checked + * @param message a message that describes the reason for the failure * @param expected the expected value * @param actual the actual value */ diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java index a7fb7249f5b..846a36ddada 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,8 +26,8 @@ import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.ResultMatcher; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertNotNull; import static org.springframework.test.util.AssertionErrors.assertEquals; +import static org.springframework.test.util.AssertionErrors.assertNotNull; import static org.springframework.test.util.AssertionErrors.assertTrue; /**