long blogs

进一步有进一步惊喜


  • Home
  • Archive
  • Tags
  •  

© 2025 long

Theme Typography by Makito

Proudly published with Hexo

单元测试unit

Posted at 2019-07-25 笔记 

要求

1、不用捕获异常
2、不要输出控制台
3、使用断言

常用的断言方法

1、assertArrayEquals(expecteds, actuals) 数组相等判断
2、assertEquals(expected, actual) 对象相等判断
3、assertNotEquals(first, second) 不相等判断
4、assertNull(object) 空值断言
5、assertNotNull(object) 非空值断言
6、assertSame(expected, actual) 对象引用相等断言
7、assertNotSame(unexpected, actual) 对象引用不相等断言
8、assertTrue(condition) 结果为真值断言
9、assertFalse(condition) 结果为假断言
10、assertThat(actual, matcher) 条件断言
11、fail() 强行失败

配置测试环境

1、maven引入依赖

1
2
3
4
5
6
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

2、test包中使用函数头使用@Test便可以使用该函数来完成单元测试

1
2
3
4
5
6
@Test
public void factorial() {
int n = 5;
int result = Solution.factorial(n);
assertEquals(120,result);
}

Share 

 Previous post: java反射和动态代理 Next post: Mybatis笔记 

© 2025 long

Theme Typography by Makito

Proudly published with Hexo