使用hamcrest增强testng的测试能力

来源:互联网 发布:ubuntu压缩文件命令 编辑:程序博客网 时间:2024/06/05 21:09
package org.pzy.permission.service.impl;import org.hamcrest.Matchers;import org.junit.Assert;import org.junit.runner.RunWith;import org.pzy.permission.service.UserServiceI;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;import org.testng.annotations.Test;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:beans-comm.xml", "classpath:beans-dao.xml","classpath:beans-security.xml" })public class HamcrestTest2 extends AbstractTestNGSpringContextTests {@Autowiredprivate UserServiceI userService;/** * 非空测试 */public void test_0() {Assert.assertThat(userService, Matchers.notNullValue());}/** * 测试数字相等 */@Testpublic void test_1() {Assert.assertThat(1, Matchers.equalTo(1));}/** * 大于测试(测试2是否大于1) */@Testpublic void test_2() {Assert.assertThat(2, Matchers.greaterThan(1));}/** * 大于等于测试 */@Testpublic void test_3() {Assert.assertThat(2, Matchers.greaterThanOrEqualTo(2));Assert.assertThat(2, Matchers.greaterThanOrEqualTo(1));}/** * 范围测试(2处于1-3这个区间) */@Testpublic void test_4() {Assert.assertThat(2, Matchers.allOf(Matchers.greaterThan(1), Matchers.lessThan(3)));}@Testpublic void test_5() {Assert.assertThat(2, Matchers.anyOf(Matchers.greaterThan(1), Matchers.lessThan(3)));}}

0 0