junit 使用教程

junit 使用教程

ID:44172028

大小:303.51 KB

页数:35页

时间:2019-10-19

junit 使用教程_第1页
junit 使用教程_第2页
junit 使用教程_第3页
junit 使用教程_第4页
junit 使用教程_第5页
资源描述:

《junit 使用教程》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、几乎所有程序员都听说过Junit的大名,但不知真正懂得运用它的人有多少,我便是其中的一个小白。知道Junit是用来测试的,但却把“宝刀”当成了“菜刀”用。为了从此不再菜鸟,特此总结整理了下Junit的知识点。1.建立Junit测试类1.右击test测试包,选择New-->Oher... 2.在窗口中找到Junit,选择JunitTestCase3.输入名称(Name),命名规则一般建议采用:类名+Test。Browse...选择要测试的类,这里是StudentService。4.勾选要测试的方法5.生成后,效果如下:这里importstatic是引入Assert类中静态属

2、性或静态方法的写法。原来要Assert.fail(),现在只需直接fial()即可,即省略了Assert。 其实不通过Junit新建向导来建立也可以,随便建立一个新类后,只需在方法上加入@Test注解即可。1.核心——断言断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过。1.断言核心方法assertArrayEquals(expecteds,actuals)查看两个数组是否相等。assertEquals(expected,actual)查看两个对象是否相等。类似于字符串比较使用的equals()方法assertNotEquals(

3、first,second)查看两个对象是否不相等。assertNull(object)查看对象是否为空。assertNotNull(object)查看对象是否不为空。assertSame(expected,actual)查看两个对象的引用是否相等。类似于使用“==”比较两个对象assertNotSame(unexpected,actual)查看两个对象的引用是否不相等。类似于使用“!=”比较两个对象assertTrue(condition)查看运行结果是否为true。assertFalse(condition)查看运行结果是否为false。assertThat(actua

4、l,matcher)查看实际值是否满足指定的条件fail()让测试失败2.示例[java]viewplaincopyprint?package test;    import static org.hamcrest.CoreMatchers.*;  import static org.junit.Assert.*;    import java.util.Arrays;    import org.hamcrest.core.CombinableMatcher;  import org.junit.Test;    public class AssertTests {  

5、        @Test        public void testAssertArrayEquals() {          byte[] expected = "trial".getBytes();          byte[] actual = "trial".getBytes();          org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);        }          @Test        public voi

6、d testAssertEquals() {          org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);        }          @Test        public void testAssertFalse() {          org.junit.Assert.assertFalse("failure - should be false", false);        }          @Test        public void testAss

7、ertNotNull() {          org.junit.Assert.assertNotNull("should not be null", new Object());        }          @Test        public void testAssertNotSame() {          org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());        }   

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。