junit使用第一弹
知识点——断言
断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过。
1. 断言核心方法
| assertArrayEquals(expecteds, actuals) | 查看两个数组是否相等。 |
| assertEquals(expected, actual) | 查看两个对象是否相等。类似于字符串比较使用的equals()方法 |
| assertNotEquals(first, second) | 查看两个对象是否不相等。 |
| assertNull(object) | 查看对象是否为空。 |
| assertNotNull(object) | 查看对象是否不为空。 |
| assertSame(expected, actual) | 查看两个对象的引用是否相等。类似于使用“==”比较两个对象 |
| assertNotSame(unexpected, actual) | 查看两个对象的引用是否不相等。类似于使用“!=”比较两个对象 |
| assertTrue(condition) | 查看运行结果是否为true。 |
| assertFalse(condition) | 查看运行结果是否为false。 |
| assertThat(actual, matcher) | 查看实际值是否满足指定的条件 |
| fail() | 让测试失败 |
2. 示例
- 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 {
- @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 void 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 testAssertNotNull() {
- 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());
- }
- @Test
- public void testAssertNull() {
- org.junit.Assert.assertNull("should be null", null);
- }
- @Test
- public void testAssertSame() {
- Integer aNumber = Integer.valueOf(768);
- org.junit.Assert.assertSame("should be same", aNumber, aNumber);
- }
- // JUnit Matchers assertThat
- @Test
- public void testAssertThatBothContainsString() {
- org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
- }
- @Test
- public void testAssertThathasItemsContainsString() {
- org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
- }
- @Test
- public void testAssertThatEveryItemContainsString() {
- org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
- }
- // Core Hamcrest Matchers with assertThat
- @Test
- public void testAssertThatHamcrestCoreMatchers() {
- assertThat("good", allOf(equalTo("good"), startsWith("good")));
- assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
- assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
- assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
- assertThat(new Object(), not(sameInstance(new Object())));
- }
- }
知识点——注解
1. 说明
| @Before | 初始化方法 |
| @After | 释放资源 |
| @Test | 测试方法,在这里可以测试期望异常和超时时间 |
| @Ignore | 忽略的测试方法 |
| @BeforeClass | 针对所有测试,只执行一次,且必须为static void |
| @AfterClass | 针对所有测试,只执行一次,且必须为static void |
| @RunWith | 指定测试类使用某个运行器 |
| @Parameters | 指定测试类的测试数据集合 |
| @Rule | 允许灵活添加或重新定义测试类中的每个测试方法的行为 |
| @FixMethodOrder | 指定测试方法的执行顺序 |
2. 执行顺序
一个测试类单元测试的执行顺序为:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一个测试方法的调用顺序为:
@Before –> @Test –> @After
3. 示例
- package test;
- import static org.junit.Assert.*;
- import org.junit.*;
- public class JDemoTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- System.out.println("in BeforeClass================");
- }
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- System.out.println("in AfterClass=================");
- }
- @Before
- public void before() {
- System.out.println("in Before");
- }
- @After
- public void after() {
- System.out.println("in After");
- }
- @Test(timeout = 10000)
- public void testadd() {
- JDemo a = new JDemo();
- assertEquals(6, a.add(3, 3));
- System.out.println("in Test ----Add");
- }
- @Test
- public void testdivision() {
- JDemo a = new JDemo();
- assertEquals(3, a.division(6, 2));
- System.out.println("in Test ----Division");
- }
- @Ignore
- @Test
- public void test_ignore() {
- JDemo a = new JDemo();
- assertEquals(6, a.add(1, 5));
- System.out.println("in test_ignore");
- }
- @Test
- public void teest_fail() {
- fail();
- }
- }
- class JDemo extends Thread {
- int result;
- public int add(int a, int b) {
- try {
- sleep(1000);
- result = a + b;
- } catch (InterruptedException e) {
- }
- return result;
- }
- public int division(int a, int b) {
- return result = a / b;
- }
- }
执行结果:
- in BeforeClass================
- in Before
- in Test ----Add
- in After
- in Before
- in Test ----Division
- in After
- in AfterClass=================
图中左上红框中部分表示Junit运行结果,5个成功(1个忽略),1个错误,1个失败。(注意错误和失败不是一回事,错误说明代码有错误,而失败表示该测试方法测试失败)
左下红框中则表示出了各个测试方法的运行状态,可以看到成功、错误、失败、失败各自的图标是不一样的,还可以看到运行时间。
右边部分则是异常堆栈,可查看异常信息。
junit使用第一弹的更多相关文章
- 我的长大app开发教程第一弹:Fragment布局
在接下来的一段时间里我会发布一个相对连续的Android教程,这个教程会讲述我是如何从零开始开发“我的长大”这个Android应用. 在开始之前,我先来介绍一下“我的长大”:这是一个校园社交app,准 ...
- typecho流程原理和插件机制浅析(第一弹)
typecho流程原理和插件机制浅析(第一弹) 兜兜 393 2014年03月28日 发布 推荐 5 推荐 收藏 24 收藏,3.5k 浏览 虽然新版本0.9在多次跳票后终于发布了,在漫长的等待里始终 ...
- Hadoop基础-MapReduce的工作原理第一弹
Hadoop基础-MapReduce的工作原理第一弹 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在本篇博客中,我们将深入学习Hadoop中的MapReduce工作机制,这些知识 ...
- Java基础-程序流程控制第一弹(分支结构/选择结构)
Java基础-程序流程控制第一弹(分支结构/选择结构) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.if语句 1>.if语句的第一种格式 if(条件表达式){ 语句体: ...
- RMQ_第一弹_Sparse Table
title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...
- codechef 营养题 第一弹
第一弾が始まる! 定期更新しない! 来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5 ...
- 好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字
原文:好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csd ...
- [Git] 002 初识 Git 与 GitHub 之加入文件 第一弹
在 GitHub 的 UI 界面使用 Git 往仓库里加文件 第一弹 1. 点击右上方的 Create new file 2. 在左上方填入文件名,若有后缀,记得加上 3. 页面跳转,此时已有两个文件 ...
- [Markdown] 03 进阶语法 第一弹
目录 1. YMAL 题头 2. 缩写 3. 强调 4. 自定义 <div> 标签 5. <cite> 标签 5. <code> 与 <br> 标签 6 ...
随机推荐
- spring-boot启动自动执行sql文件失效 解决办法
在springboot1.5及以前的版本,要执行sql文件只需在applicaion文件里指定sql文件的位置即可.但是到了springboot2.x版本, 如果只是这样做的话springboot不会 ...
- 拓扑排序(Topological Order)
Date:2019-06-17 14:43:59 算法描述 1.定义队列Q,并把所有入度为0的结点加入队列 2.取队首结点,输出.然后删除所有从它除法的边,并令这些边到达的顶点的入度-1,若某个顶点的 ...
- Hbuider sass配置 webstorm scss配置
--no-cache %FileName% ../css/%FileBaseName%.css sass编译后保存到css目录下 webstorm scss配置 C:\Ruby22-x64\bin ...
- pytorch实战(7)-----卷积神经网络
一.卷积: 卷积在 pytorch 中有两种方式: [实际使用中基本都使用 nn.Conv2d() 这种形式] 一种是 torch.nn.Conv2d(), 一种是 torch.nn.function ...
- html第六节课
JavaScript 一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系 ...
- C#第二节课
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- malloc实现机制、缓冲机制、文件操作、mmap虚拟地址(day06)
一.malloc的实现机制(缓冲机制) 库函数跟系统调用之间的关系 什么是缓冲? 内存分配的原理. 封装 函数A的实现代码中调用了函数B.函数B的功能是函数A主要的功能,这样就说函数A封装了函数B. ...
- redis liunx系统安装
同事总结非常好,借鉴一下 原文地址:https://www.cnblogs.com/dslx/p/9291535.html redis安装 下载redis的安装包上传到Linux服务器,安装包如下 h ...
- poj 3006水题打素数表
#include<stdio.h> #include<string.h> #define N 1100000 int isprim[N],prime[N]; void ispr ...
- HDU - 1043 - Eight / POJ - 1077 - Eight
先上题目: Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...