Browse Source

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
pull/23837/head
Sam Brannen 6 years ago
parent
commit
233b225b4f
  1. 38
      spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java
  2. 4
      spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java

38
spring-test/src/main/java/org/springframework/test/util/AssertionErrors.java

@ -1,5 +1,5 @@ @@ -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; @@ -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.
* <p>For example given:
* <pre class="code">
@ -47,9 +48,9 @@ public abstract class AssertionErrors { @@ -47,9 +48,9 @@ public abstract class AssertionErrors {
* <pre class="code">
* Response header [Accept] expected:&lt;application/json&gt; but was:&lt;text/plain&gt;
* </pre>
* @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 { @@ -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 { @@ -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.
* <p>For example:
* <pre class="code">
* assertEquals("Response header [" + name + "]", expected, actual);
* </pre>
* @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 { @@ -89,7 +101,7 @@ public abstract class AssertionErrors {
* <pre class="code">
* assertNotEquals("Response header [" + name + "]", expected, actual);
* </pre>
* @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
*/

4
spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java

@ -1,5 +1,5 @@ @@ -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; @@ -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;
/**

Loading…
Cancel
Save