diff --git a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java
index f49b30551c0..4eda8a45ac2 100644
--- a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java
+++ b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java
@@ -18,6 +18,7 @@ package org.springframework.test.util;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
+import org.springframework.util.Assert;
/**
* {@code AopTestUtils} is a collection of AOP-related utility methods for
@@ -41,7 +42,8 @@ public class AopTestUtils {
* be returned; otherwise, the {@code candidate} will be returned
* as is.
*
- * @param candidate the instance to check (potentially a Spring AOP proxy)
+ * @param candidate the instance to check (potentially a Spring AOP proxy);
+ * never {@code null}
* @return the target object or the {@code candidate}; never {@code null}
* @throws IllegalStateException if an error occurs while unwrapping a proxy
* @see Advised#getTargetSource()
@@ -49,6 +51,7 @@ public class AopTestUtils {
*/
@SuppressWarnings("unchecked")
public static T getTargetObject(Object candidate) {
+ Assert.notNull(candidate, "candidate must not be null");
try {
if (AopUtils.isAopProxy(candidate) && (candidate instanceof Advised)) {
return (T) ((Advised) candidate).getTargetSource().getTarget();
@@ -57,8 +60,6 @@ public class AopTestUtils {
catch (Exception e) {
throw new IllegalStateException("Failed to unwrap proxied object.", e);
}
-
- // else
return (T) candidate;
}
@@ -71,7 +72,8 @@ public class AopTestUtils {
* nested proxies will be returned; otherwise, the {@code candidate}
* will be returned as is.
*
- * @param candidate the instance to check (potentially a Spring AOP proxy)
+ * @param candidate the instance to check (potentially a Spring AOP proxy);
+ * never {@code null}
* @return the ultimate target object or the {@code candidate}; never
* {@code null}
* @throws IllegalStateException if an error occurs while unwrapping a proxy
@@ -80,6 +82,7 @@ public class AopTestUtils {
*/
@SuppressWarnings("unchecked")
public static T getUltimateTargetObject(Object candidate) {
+ Assert.notNull(candidate, "candidate must not be null");
try {
if (AopUtils.isAopProxy(candidate) && (candidate instanceof Advised)) {
return (T) getUltimateTargetObject(((Advised) candidate).getTargetSource().getTarget());
@@ -88,8 +91,6 @@ public class AopTestUtils {
catch (Exception e) {
throw new IllegalStateException("Failed to unwrap proxied object.", e);
}
-
- // else
return (T) candidate;
}
diff --git a/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java b/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java
index 21070c5646a..dac809f4ea4 100644
--- a/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java
+++ b/spring-test/src/test/java/org/springframework/test/util/AopTestUtilsTests.java
@@ -36,6 +36,11 @@ public class AopTestUtilsTests {
private final FooImpl foo = new FooImpl();
+ @Test(expected = IllegalArgumentException.class)
+ public void getTargetObjectForNull() {
+ getTargetObject(null);
+ }
+
@Test
public void getTargetObjectForNonProxiedObject() {
Foo target = getTargetObject(foo);
@@ -66,6 +71,11 @@ public class AopTestUtilsTests {
assertNotSame(foo, target);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void getUltimateTargetObjectForNull() {
+ getUltimateTargetObject(null);
+ }
+
@Test
public void getUltimateTargetObjectForNonProxiedObject() {
Foo target = getUltimateTargetObject(foo);