当前位置: 首页>>代码示例>>Java>>正文


Java AssertThrows类代码示例

本文整理汇总了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();
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:69,代码来源:ReflectionTestUtilsTests.java


注:本文中的org.springframework.test.AssertThrows类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。