Skip to content

Commit 7bbb4ec

Browse files
committed
Fix Assert.instanceOf exception message
Update the exception message used when Assert.instanceOf fails such that it expects the provided message to end with '.'. This reverts commit 5874383 which caused the implementation to be at odds with the JavaDoc and the previous release. The updated code also has the benefit of protecting against a null message. Issue: SPR-10269
1 parent 2aaa66f commit 7bbb4ec

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

spring-core/src/main/java/org/springframework/util/Assert.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -334,8 +334,9 @@ public static void isInstanceOf(Class clazz, Object obj) {
334334
public static void isInstanceOf(Class type, Object obj, String message) {
335335
notNull(type, "Type to check against must not be null");
336336
if (!type.isInstance(obj)) {
337-
throw new IllegalArgumentException(message +
338-
". Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
337+
throw new IllegalArgumentException(
338+
(StringUtils.hasLength(message) ? message + " " : "") +
339+
"Object of class [" + (obj != null ? obj.getClass().getName() : "null") +
339340
"] must be an instance of " + type);
340341
}
341342
}

spring-core/src/test/java/org/springframework/util/AssertTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,9 @@
2424
import java.util.Map;
2525
import java.util.Set;
2626

27+
import org.junit.Rule;
2728
import org.junit.Test;
29+
import org.junit.rules.ExpectedException;
2830

2931
/**
3032
* Unit tests for the {@link Assert} class.
@@ -36,13 +38,32 @@
3638
*/
3739
public class AssertTests {
3840

41+
@Rule
42+
public ExpectedException thrown = ExpectedException.none();
43+
3944
@Test(expected = IllegalArgumentException.class)
4045
public void instanceOf() {
4146
final Set<?> set = new HashSet<Object>();
4247
Assert.isInstanceOf(HashSet.class, set);
4348
Assert.isInstanceOf(HashMap.class, set);
4449
}
4550

51+
@Test
52+
public void instanceOfNoMessage() throws Exception {
53+
thrown.expect(IllegalArgumentException.class);
54+
thrown.expectMessage("Object of class [java.lang.Object] must be an instance " +
55+
"of interface java.util.Set");
56+
Assert.isInstanceOf(Set.class, new Object(), null);
57+
}
58+
59+
@Test
60+
public void instanceOfMessage() throws Exception {
61+
thrown.expect(IllegalArgumentException.class);
62+
thrown.expectMessage("Custom message. Object of class [java.lang.Object] must " +
63+
"be an instance of interface java.util.Set");
64+
Assert.isInstanceOf(Set.class, new Object(), "Custom message.");
65+
}
66+
4667
@Test
4768
public void isNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() {
4869
Assert.isNull(null, "Bla");

0 commit comments

Comments
 (0)