本文整理汇总了Java中org.springframework.test.AssertThrows类的典型用法代码示例。如果您正苦于以下问题:Java AssertThrows类的具体用法?Java AssertThrows怎么用?Java AssertThrows使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AssertThrows类属于org.springframework.test包,在下文中一共展示了AssertThrows类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setAndGetFields
import org.springframework.test.AssertThrows; //导入依赖的package包/类
@Test
public void setAndGetFields() throws Exception {
// ---------------------------------------------------------------------
// Standard
setField(person, "id", new Long(99), long.class);
setField(person, "name", "Tom");
setField(person, "age", new Integer(42));
setField(person, "eyeColor", "blue", String.class);
setField(person, "likesPets", Boolean.TRUE);
setField(person, "favoriteNumber", PI, Number.class);
assertEquals("ID (private field in a superclass)", 99, person.getId());
assertEquals("name (protected field)", "Tom", person.getName());
assertEquals("age (private field)", 42, person.getAge());
assertEquals("eye color (package private field)", "blue", person.getEyeColor());
assertEquals("'likes pets' flag (package private boolean field)", true, person.likesPets());
assertEquals("'favorite number' (package field)", PI, person.getFavoriteNumber());
assertEquals(new Long(99), getField(person, "id"));
assertEquals("Tom", getField(person, "name"));
assertEquals(new Integer(42), getField(person, "age"));
assertEquals("blue", getField(person, "eyeColor"));
assertEquals(Boolean.TRUE, getField(person, "likesPets"));
assertEquals(PI, getField(person, "favoriteNumber"));
// ---------------------------------------------------------------------
// Null - non-primitives
setField(person, "name", null, String.class);
setField(person, "eyeColor", null, String.class);
setField(person, "favoriteNumber", null, Number.class);
assertNull("name (protected field)", person.getName());
assertNull("eye color (package private field)", person.getEyeColor());
assertNull("'favorite number' (package field)", person.getFavoriteNumber());
// ---------------------------------------------------------------------
// Null - primitives
new AssertThrows(IllegalArgumentException.class,
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") {
@Override
public void test() throws Exception {
setField(person, "id", null, long.class);
}
}.runTest();
new AssertThrows(IllegalArgumentException.class,
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") {
@Override
public void test() throws Exception {
setField(person, "age", null, int.class);
}
}.runTest();
new AssertThrows(IllegalArgumentException.class,
"Calling setField() with NULL for a primitive type should throw an IllegalArgumentException.") {
@Override
public void test() throws Exception {
setField(person, "likesPets", null, boolean.class);
}
}.runTest();
}