Changed use of AssertThrows to @Test(expected = ...)

This commit is contained in:
Arjen Poutsma 2008-10-31 08:57:10 +00:00
parent 19debfb080
commit 2cf6dc4480
1 changed files with 104 additions and 133 deletions

View File

@ -16,10 +16,15 @@
package org.springframework.util; package org.springframework.util;
import junit.framework.TestCase; import java.util.ArrayList;
import org.springframework.test.AssertThrows; import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*; import org.junit.Test;
/** /**
* Unit tests for the {@link Assert} class. * Unit tests for the {@link Assert} class.
@ -27,158 +32,124 @@ import java.util.*;
* @author Keith Donald * @author Keith Donald
* @author Erwin Vervaet * @author Erwin Vervaet
* @author Rick Evans * @author Rick Evans
* @author Arjen Poutsma
*/ */
public class AssertTests extends TestCase { public class AssertTests {
public void testInstanceOf() { @Test(expected = IllegalArgumentException.class)
final Set set = new HashSet(); public void instanceOf() {
Assert.isInstanceOf(HashSet.class, set); final Set set = new HashSet();
new AssertThrows(IllegalArgumentException.class, "a hash map is not a set") { Assert.isInstanceOf(HashSet.class, set);
public void test() throws Exception { Assert.isInstanceOf(HashMap.class, set);
Assert.isInstanceOf(HashMap.class, set); }
}
}.runTest();
}
public void testIsNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() { @Test
Assert.isNull(null, "Bla"); public void isNullDoesNotThrowExceptionIfArgumentIsNullWithMessage() {
} Assert.isNull(null, "Bla");
}
public void testIsNullDoesNotThrowExceptionIfArgumentIsNull() { @Test
Assert.isNull(null); public void isNullDoesNotThrowExceptionIfArgumentIsNull() {
} Assert.isNull(null);
}
public void testIsNullThrowsExceptionIfArgumentIsNotNull() { @Test(expected = IllegalArgumentException.class)
new AssertThrows(IllegalArgumentException.class, "object is not null") { public void isNullThrowsExceptionIfArgumentIsNotNull() {
public void test() throws Exception { Assert.isNull(new Object());
Assert.isNull(new Object()); }
}
}.runTest();
}
public void testIsNullThrowsExceptionIfArgumentIsNotNullWithMessage() { @Test(expected = IllegalArgumentException.class)
new AssertThrows(IllegalArgumentException.class, "object is not null") { public void isTrueWithFalseExpressionThrowsException() throws Exception {
public void test() throws Exception { Assert.isTrue(false);
Assert.isNull(new Object(), "Bla"); }
}
protected void checkExceptionExpectations(Exception actualException) { @Test
assertEquals("Bla", actualException.getMessage()); public void isTrueWithTrueExpressionSunnyDay() throws Exception {
super.checkExceptionExpectations(actualException); Assert.isTrue(true);
} }
}.runTest();
}
public void testIsTrueWithFalseExpressionThrowsException() throws Exception { @Test(expected = IllegalArgumentException.class)
new AssertThrows(IllegalArgumentException.class) { public void testHasLengthWithNullStringThrowsException() throws Exception {
public void test() throws Exception { Assert.hasLength(null);
Assert.isTrue(false); }
}
}.runTest();
}
public void testIsTrueWithTrueExpressionSunnyDay() throws Exception { @Test(expected = IllegalArgumentException.class)
Assert.isTrue(true); public void hasLengthWithEmptyStringThrowsException() throws Exception {
} Assert.hasLength("");
}
public void testHasLengthWithNullStringThrowsException() throws Exception { @Test
new AssertThrows(IllegalArgumentException.class) { public void hasLengthWithWhitespaceOnlyStringDoesNotThrowException() throws Exception {
public void test() throws Exception { Assert.hasLength("\t ");
Assert.hasLength(null); }
}
}.runTest();
}
public void testHasLengthWithEmptyStringThrowsException() throws Exception { @Test
new AssertThrows(IllegalArgumentException.class) { public void hasLengthSunnyDay() throws Exception {
public void test() throws Exception { Assert.hasLength("I Heart ...");
Assert.hasLength(""); }
}
}.runTest();
}
public void testHasLengthWithWhitespaceOnlyStringDoesNotThrowException() throws Exception { @Test
Assert.hasLength("\t "); public void doesNotContainWithNullSearchStringDoesNotThrowException() throws Exception {
} Assert.doesNotContain(null, "rod");
}
public void testHasLengthSunnyDay() throws Exception { @Test
Assert.hasLength("I Heart ..."); public void doesNotContainWithNullSubstringDoesNotThrowException() throws Exception {
} Assert.doesNotContain("A cool chick's name is Brod. ", null);
}
public void testDoesNotContainWithNullSearchStringDoesNotThrowException() throws Exception { @Test
Assert.doesNotContain(null, "rod"); public void doesNotContainWithEmptySubstringDoesNotThrowException() throws Exception {
} Assert.doesNotContain("A cool chick's name is Brod. ", "");
}
public void testDoesNotContainWithNullSubstringDoesNotThrowException() throws Exception { @Test(expected = IllegalArgumentException.class)
Assert.doesNotContain("A cool chick's name is Brod. ", null); public void assertNotEmptyWithNullCollectionThrowsException() throws Exception {
} Assert.notEmpty((Collection) null);
}
public void testDoesNotContainWithEmptySubstringDoesNotThrowException() throws Exception { @Test(expected = IllegalArgumentException.class)
Assert.doesNotContain("A cool chick's name is Brod. ", ""); public void assertNotEmptyWithEmptyCollectionThrowsException() throws Exception {
} Assert.notEmpty(new ArrayList());
}
public void testAssertNotEmptyWithNullCollectionThrowsException() throws Exception { @Test
new AssertThrows(IllegalArgumentException.class) { public void assertNotEmptyWithCollectionSunnyDay() throws Exception {
public void test() throws Exception { List<String> collection = new ArrayList<String>();
Assert.notEmpty((Collection) null); collection.add("");
} Assert.notEmpty(collection);
}.runTest(); }
}
public void testAssertNotEmptyWithEmptyCollectionThrowsException() throws Exception { @Test(expected = IllegalArgumentException.class)
new AssertThrows(IllegalArgumentException.class) { public void assertNotEmptyWithNullMapThrowsException() throws Exception {
public void test() throws Exception { Assert.notEmpty((Map) null);
Assert.notEmpty(new ArrayList()); }
}
}.runTest();
}
public void testAssertNotEmptyWithCollectionSunnyDay() throws Exception { @Test(expected = IllegalArgumentException.class)
List collection = new ArrayList(); public void assertNotEmptyWithEmptyMapThrowsException() throws Exception {
collection.add(""); Assert.notEmpty(new HashMap());
Assert.notEmpty(collection); }
}
public void testAssertNotEmptyWithNullMapThrowsException() throws Exception { @Test
new AssertThrows(IllegalArgumentException.class) { public void assertNotEmptyWithMapSunnyDay() throws Exception {
public void test() throws Exception { Map<String, String> map = new HashMap<String, String>();
Assert.notEmpty((Map) null); map.put("", "");
} Assert.notEmpty(map);
}.runTest(); }
}
public void testAssertNotEmptyWithEmptyMapThrowsException() throws Exception { @Test(expected = IllegalArgumentException.class)
new AssertThrows(IllegalArgumentException.class) { public void isInstanceofClassWithNullInstanceThrowsException() throws Exception {
public void test() throws Exception { Assert.isInstanceOf(String.class, null);
Assert.notEmpty(new HashMap()); }
}
}.runTest();
}
public void testAssertNotEmptyWithMapSunnyDay() throws Exception { @Test(expected = IllegalStateException.class)
Map map = new HashMap(); public void stateWithFalseExpressionThrowsException() throws Exception {
map.put("", ""); Assert.state(false);
Assert.notEmpty(map); }
}
public void testIsInstanceofClassWithNullInstanceThrowsException() throws Exception { @Test
new AssertThrows(IllegalArgumentException.class) { public void stateWithTrueExpressionSunnyDay() throws Exception {
public void test() throws Exception { Assert.state(true);
Assert.isInstanceOf(String.class, null); }
}
}.runTest();
}
public void testStateWithFalseExpressionThrowsException() throws Exception {
new AssertThrows(IllegalStateException.class) {
public void test() throws Exception {
Assert.state(false);
}
}.runTest();
}
public void testStateWithTrueExpressionSunnyDay() throws Exception {
Assert.state(true);
}
} }