[1]Junit4-Assertions的使用
来源:互联网 发布:移动硬盘不能写入 mac 编辑:程序博客网 时间:2024/05/22 06:46
官网:http://junit.org/junit4/
想要减少程序的Bug?
Junit的作用就是一个测试代码工具,使用它我们可以很简单地进行测试驱动开发,减少代码Bug。
maven配置:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope></dependency>
package com.mjj.test;import org.hamcrest.CoreMatchers;import org.junit.Test;import java.util.Arrays;import static org.junit.Assert.*;public class TestAssertions { @Test public void testAssertArrayEquals(){ int [] expected=new int[2]; expected[0]=1; expected[1]=2; int [] actual=new int [2]; actual[0]=1; actual[1]=2; //数组内容必须一致 assertArrayEquals("array must equals",expected,actual); Object[] expectedObjArray=new Object[1]; expectedObjArray[0]=new java.lang.Object(); Object[] actualObjArray=new Object[1]; actualObjArray[0]=new java.lang.Object(); //数组内容必须一致,对象验证必须是同一个对象 assertArrayEquals("array must equals,Object must be same",expectedObjArray,actualObjArray); } @Test public void testAssertEquals(){ assertEquals("string must be equals:","s","s"); } @Test public void testAssertNotNull(){ assertNotNull("object must not null",null); } @Test public void testAssertNull(){ assertNull("object must null", new Object()); } @Test public void testAssertNotSame(){ assertNotSame("value should not be same","s","s"); } @Test public void textAssertSame(){ assertSame("value should be same",new Object(),new Object()); } @Test public void testAssertBooleanEquals(){ assertFalse("should be false", true); } @Test public void testAssertThatBothContainsString(){ //Matchers匹配模式,功能比较强大 assertThat("albumen", CoreMatchers.both(CoreMatchers.containsString("a")).and(CoreMatchers.containsString("n"))); } @Test public void testAssertThatHasItems(){ //验证list是否包含元素 assertThat(Arrays.asList("one","two","three"),CoreMatchers.hasItems("two","one")); }}
看完以上代码是否觉得简单,觉得可有可无?有时候我们程序的Bug是因为我们的大意和太过自信,没在实际环境运行过的代码我们不能100%保证无Bug,更何况是简单的测试?一行代码出错就是错,不要小看每一行代码。
0 0
- [1]Junit4-Assertions的使用
- assertions 使用问答
- SDL 错误处理和断言的使用(Assertions)
- JUnit4的使用
- JUnit4的使用
- JUnit4的使用
- Junit4的使用
- Junit4--hamcrest的使用
- junit4的使用
- Junit4 -- TestSuite 的使用
- JUnit4的简单使用
- Assertions
- Assertions
- Junit4的使用及实例
- JUnit4的使用和配置
- Junit4测试套件的使用
- junit4单元测试框架的使用
- Junit4-使用JUnit4
- EPOLL模型
- SurfaceView相关
- 关于Iterator接口(迭代器)
- hive优化之-控制hive任务中的map数和reduce数
- VM虚拟机-Linux虚拟机系统
- [1]Junit4-Assertions的使用
- Linux-ntp&ftp
- java基础面试常见问题总结
- java/c# 判断点是否在多边形区域内
- 通讯录(一) —————— 静态实现
- 复习布局基础
- NS3 - queue-discs-benchmark.cc - traffic-control-module.h - flow-monitor-module.h
- 数组中添加对象
- 洗牌算法和随机数生成