Junit 4

JUnit实战篇 JUnit实战篇 (一)???????????????????????????????????????????????????????????????????????????? 修订者:黄 凯???????E_mail:hk_sz@163.com前言由于现在公司进行Unit Test Case的整理阶段,所以抽空对Junit进行了一下了解,以下是集合了众家所长之精华(考虑的是按我的 阅读详情

JUnit 4.0 introduces a completely different API to the older versions. JUnit 4.0 uses Java 5.0 annotations to describe tests instead of using inheritence. It introduces more flexible initialization and cleanup, timeouts, and parameterized test cases. This post describes the new features in JUnit 4.0, and in the end, I show a basic example that tests the java.util.Stack class.

1)The tests: Unlike in JUnit 3.x you don't have to extend TestCase to implement tests. A simple Java class can be used as a TestCase. The test methods have to be simply annotated with org.junit.Test annotation as shown below


@Test
public void emptyTest() {
 stack = new Stack<String>();
 assertTrue(stack.isEmpty());
}

2)Using Assert Methods: In JUnit 4.0 test classes do not inherit from TestCase, as a result, the Assert methods are not available to the test classes. In order to use the Assert methods, you have to use either the prefixed syntax (Assert.assertEquals()) or, use a static import for the Assert class.


import static org.junit.Assert.*;
Now the assert methods may be used directly as done with the previous versions of JUnit.

3)Changes in Assert Methods: The new assertEquals methods use Autoboxing, and hence all the assertEquals(primitive, primitive) methods will be tested as assertEquals(Object, Object). This may lead to some interesting results. For example autoboxing will convert all numbers to the Integer class, so an Integer(10) may not be equal to Long(10). This has to be considered when writing tests for arithmetic methods. For example, the following Calc class and it's corresponding test CalcTest will give you an error.


public class Calc {
 public long add(int a, int b) {
  return a+b;
 }
}

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CalcTest {
 @Test
 public void testAdd() {
  assertEquals(5, new Calc().add(2, 3));
 }
}
You will end up with the following error.

java.lang.AssertionError: expected:<5> but was:<5>
This is due to autoboxing. By default all the integers are cast to Integer, but we were expecting long here. Hence the error. In order to overcome this problem, it is better if you type cast the first parameter in the assertEquals to the appropriate return type for the tested method as follows

assertEquals((long)5, new Calc().add(2, 3));
There are also a couple of methods for comparing Arrays

public static void assertEquals(String message, Object[] expecteds, Object[] actuals);
public static void assertEquals(Object[] expecteds, Object[] actuals);

4)Setup and TearDown: You need not have to create setup and teardown methods for setup and teardown. The @Before, @After and @BeforeClass, @AfterClass annotations are used for implementing setup and teardown operations. The @Before and @BeforeClass methods are run before running the tests. The @After and @AfterClass methods are run after the tests are run. The only difference being that the @Before and @After can be used for multiple methods in a class, but the @BeforeClass and @AfterClass can be used only once per class.

5)Parameterized Tests: JUnit 4.0 comes with another special runner: Parameterized, which allows you to run the same test with different data. For example, in the the following peice of code will imply that the tests will run four times, with the parameter "number" changed each time to the value in the array.


@RunWith(value = Parameterized.class)
public class StackTest {
 Stack<Integer> stack;
 private int number;

 public StackTest(int number) {
   this.number = number;
 }

 @Parameters
 public static Collection data() {
   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);
 }
 ...
}
The requirement for parameterized tests is to
Have the annotation @RunWith for the Test Class
Have a public static method that returns a Collection for data. Each element of the collection must be an Array of the various paramters used for the test.
You will also need a public constructor that uses the parameters

6)Test Suites: In JUnit 3.8 you had to add a suite() method to your classes, to run all tests as a suite. With JUnit 4.0 you use annotations instead. To run the CalculatorTest and SquareTest you write an empty class with @RunWith and @Suite annotations.


import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({StackTest.class})
public class AllTests {
}
The "Suite" class takes SuiteClasses as argument which is a list of all the classes that can be run in the suite.
The following is a listing of the example StackTest used in the post.

package tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.EmptyStackException;
import java.util.Stack;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class StackTest {
 Stack<Integer> stack;

 private int number;

 public StackTest(int number) {
   this.number = number;
 }

 @Parameters
 public static Collection data() {
   Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
   return Arrays.asList(data);
 }

 @Before
 public void noSetup() {
   stack = new Stack<Integer>();
 }

 @After
 public void noTearDown() {
   stack = null;
 }

 @Test
 public void pushTest() {
   stack.push(number);
   assertEquals(stack.peek(), number);

 }

 @Test
 public void popTest() {
 }

 @Test(expected = EmptyStackException.class)
 public void peekTest() {
   stack = new Stack<Integer>();
   stack.peek();
 }

 @Test
 public void emptyTest() {
   stack = new Stack<Integer>();
   assertTrue(stack.isEmpty());
 }

 @Test
 public void searchTest() {
 }

变压器铁心100Hz振动噪声的COMSOL磁致伸缩仿真全流程 磁致伸缩是铁磁材料在磁场作用下发生尺寸变化的现象,也是变压器铁心振动与噪声产生的根本原因。由于应变与磁通密度近似成平方关系,50Hz电网激励下铁心振动基频为100Hz。借助COMSOL多物理场仿真,可将磁场计算结果映射为结构应力和变形,完成“电磁—结构”单向耦合分析。该技术广泛应用于变压器振动噪声预测、结构优化及故障诊断等工程场景。本文从磁致伸缩物理机理出发,系统梳理了材料参数、等效建模、边界条件设置和结果校核等关键步骤,帮助工程师掌握从磁致伸缩到结构变形的完整仿真链路,解决实际工程对标难题。 阅读详情

相关推荐

STM32玩转C++:从Arduino库到HAL库的封装思路(Clion环境配置指南)

本文介绍了如何在STM32开发中使用C++封装HAL库,从Arduino风格过渡到更高效的开发方式。通过Clion环境配置指南和详细的代码示例,展示了如何利用C++的面向对象特性简化GPIO、UART等外设操作,提升代码可读性和开发效率。特别适合嵌入式开发者和从Arduino转向STM32的开发者。

weixin_33737134的博客 338

junit4 Rule用法

TemporaryFolder TemporaryFolder作为Rule,可以运行在测试过程中创建临时文件或者临时目录,当测试结束后,框架会自动删除。 见实例:    public static class HasTempFolder {    @Rule    public TemporaryFolder folder = new TemporaryFolde

baynkbtg的博客 4413

Linux-RGMII PHY 88E1512 驱动调试记录

1. 简介 1)开发板平台:创龙 AM5728 (tl5728-easy-evm) 2)PHY芯片:Marvell 88E1512 88E1512和88E1510都是一个系列的phy,采用marvell的通用phy驱动,驱动源码路径如下:drivers/net/phy/marvell.c 2. 应用场景 1)系统框图 AM5728连接两个PHY,PHY0的工作模式是 RGMII to Copper (RGMII到电口),PHY1的工作模式是 RGMII to SGMII。 两个PHY与处理器的连接方式都是

weixin_40407893的博客 2万+

Android学习 Eclipse Android 单元测试

参考自:http://blog.sina.com.cn/s/blog_90cdca4c01015wow.html Android 采用的单元测试框架为android 自带的junit,具体见Android API。 单元测试:必须有个预期输出值,有个实际输出值,验证预期输出值与实际输出值是否相同,因此被测试的方法肯定会返回一个值 Eclipse下的单元测试,是在模拟器中运行的,因此即使测试某

Danny Chen的专栏 798

机器学习教程 之 半监督学习 Co-training 协同训练 (论文、算法、数据集、代码)

这篇博客介绍的是一篇用于半监督分类问题的方法: 协同训练 Co-training, A. Blum and T. Mitchell, “Combining labeled and unlabeled data with co-training,” in Proc. of the 11th Annual Conf. on Computational Learning Theory, 1998.,这算是半监督领域里面始祖级的论文了,是1998年两位CMU大佬提出的方法。 之前还写过一篇关于Tri-trainin

冯良骏 的 博客 1万+

CubeMX配置ADC+DMA避坑实战:如何避免采样过快导致的系统崩溃

本文针对STM32开发中常见的ADC采样过快导致程序卡死问题,提供了基于CubeMX的实战避坑指南。通过分析ADC与DMA协作机制,详解时钟配置、采样时间、DMA缓冲区长度及中断设置等关键步骤,帮助开发者从源头规避系统崩溃风险,实现稳定可靠的数据采集。

weixin_28273593的博客 329

JUnit 5和JUnit 4比较

JUnit 5旨在调整java 8样式的编码,并且比JUnit 4更强大和灵活。在这篇文章中,JUnit 5 vs JUnit 4,我们将关注junit 4和junit 5之间的一些主要差异。 1. JUnit 5 vs JUnit 4 - 注释 两个版本中的大多数注释都是相同的,但很少有区别。这是一个快速比较。 特征 JUNIT 4 JUNIT 5 声明一种测试方法 ...

javaZhong的博客 3万+

FC7300 Clock介绍

FC7300的时钟架构包括一个高级时钟图和内部时钟需求。它有5个CMU,使用SIRC监控FIRC/SOSC/FOSC,并使用FIRC监控SIRC。此外,CMU4还使用FOSC监Slow Clock。

舊梦的博客 330

junit5和junit4_JUnit声明异常– JUnit 5和JUnit 4

junit5和junit4We can test expected exceptions using JUnit 5 assertThrows assertion. This JUnit assertion method returns the thrown exception, so we can use it to assert exception message too. 我们可以使用JUn...

从零开始的教程世界 1529

SpringBoot4+Java21前后端分离架构项目脚手架

本文介绍了基于SpringBoot 4和Java 21的前后端分离项目架构方案。主要内容包括:1)采用Spring Security 7实现JWT无状态认证和RBAC权限控制;2)后端使用MyBatis-Plus简化数据库操作,前端采用Vue3+Pinia组合;3)详细的项目结构设计,推荐按业务模块分包;4)核心依赖配置和架构设计图,涵盖认证授权流程。该方案结合最新技术栈,提供了生产级的全链路实现,特别适合需要快速构建现代化Web应用的项目。

Hello, New World! 435

C/C++跳动的爱心(完整代码)

C语言实现跳动的爱心的完整代码。

AIGC 5848

Junit 4 与Junit 5区别

所需JDK Junit 4 Junit 5 需要 Java 5 或以上版本 需要Java 8 或以上版本 Architecture Junit 4 所有的东西在一个jar包里,maven依赖如下: junit junit 4.13.1 test Junit 5 由JUnit Platform, JUnit Jupiter 和 JUnit Vintage 三个子项目组成: JUnit Platform 定义了 test engine API,用于开发在

weixin_42258032的博客 2105

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

JUnit 4和JUnit 5区别

特征 JUNIT 4 JUNIT 5 声明一种测试方法 @Test @Test 在当前类中的所有测试方法之前执行 @BeforeClass @BeforeAll 在当前类中的所有测试方法之后执行 @AfterClass @AfterAll 在每个测试方法之前执行 @Before @BeforeEach 每种测试方法后执行 @After @AfterEach 禁用测试方法/类 @Ignore @Disa

渣渣 6262

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

JUnit 4与JUnit 5的差异详解

在进行`SpringBoot`项目单元测试时,发现有时候给类打上 `@SpringBootTest`注解就能运行项目,但有时候需要`@RunWith(SpringRunner.class)`和`@SpringBootTest`注解才能运行,你有研究过这是为什么吗?本文就来讲一下这个问题。

谷美的互联网练摊记 1585

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

junit5和junit4_混合JUnit4和JUnit5

junit5和junit4 Yomex Owo在Unsplash上拍摄的照片 JUnit是Java编程语言最受欢迎的测试框架之一。 在测试驱动开发(TDD)的开发中也很重要。 在2018年,该框架的最新版本JUnit5得以发布,许多使用JUnit平台的程序员不得不决定是否重构其在以前版本的JUnit中编写的现有代码。 本文是关于将JUnit4和JUnit5混入正在进行的项目中,以在...

编程故事的地方 975

Spring——整合junit4、junit5使用方法

Spring整合Junit4和Junit5使用方法

一起加油吧~ 2795

JUnit 4 vs. JUnit 5

JUnit是一个开源的单元测试框架,专为Java编程语言设计。它提供了一种简单的机制,使得开发者可以在编写代码时快速创建和运行测试,从而验证程序的逻辑和功能是否按预期工作。JUnit最初是由Erich Gamma和Kent Beck于1997年创建的,最早目的在于支持测试驱动开发(TDD),并为程序测试提供一个标准化的框架。JUnit的发展历程与Java语言的演进密切相关。随着Java应用的普及,尤其是在企业级应用和大型软件开发项目中,测试的重要性逐渐被认识到。

不忘的博客 1556

JUnit4

JUnit 是 Java 编程语言的单元测试框架,用于编写和运行可重复的自动化测试。JUnit 是一个开放的资源框架,用于编写和运行测试。提供注解来识别测试方法。提供断言来测试预期结果。JUnit 测试允许你编写代码更快,并能提高质量。JUnit 优雅简洁,没那么复杂,花费时间较少。JUnit 测试可以自动运行并且检查自身结果并提供即时反馈,所以也没有必要人工梳理测试结果的报告。JUnit 测试可以被组织为测试套件,包含测试用例,甚至其他的测试套件。

专注于技术分享的博主 893

JUnit4和JUnit5的主要区别

本文介绍了JUnit4和JUnit5的主要区别,用于快速了解JUnit5。

码农无双的博客 7459

JUnit4 简介

JUnit4是一个易学易用的Java单元测试框架,使用非常广泛。现阶段的最新版本号是4.12,JUnit5目前正在测试中,所以这里还是以JUnit4为准。引入JUnit现在主流的IDE比如IDEA或者Eclipse都提供了对JUnit4的支持,可以非常方便的使用JUnit4。当你在代码中添加了@Test注解,然后使用IDE的自动补全功能时,一般情况下IDE会弹出对话框询问你是否将JUnit4库添加到

乐百川 8682

junit3对比junit4

本文内容摘自junit实战,感谢作者的无私奉献。 个人觉得每个开源包的版本对比意义不大,闲来无事,这里就来整理一下好了。本文名为junit3对比junit4,但是我通过这篇博客主要也是想统一的来整理下junit4的一些功能。OK,不废话了,现在我们开始: 1,版本的向前兼容和向后兼容 所有的junit4测试执行器runner都能够执行junit3测试,代码不需要进行任何修改。但是使用juni

LinkinPark的博客 688

​​JUnit 5的使用以及JUnit4和JUnit 5的区别

测试:是一种用来促进鉴定软件的正确性、完整性、安全性和质量的过程。

xrkhy的博客 914

JUnit3和Junit4简介

JUnit是一个开发源代码的Java测试框架,用于编写和运行可重复的测试。

坚定不移的蜗牛的专栏 752

JUnit5 与 JUnit4 区别及及新特性

Junit5架构与简述 Junit5 与以前的JUnit版本不同,JUnit 5由来自三个不同子项目的几个不同模块组成。 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage JUnit Platform 作用于JVM上启动测试框架。还定义了开发在平台上运行的测试框架的TestEngine API。开发或测试人员可从命令行启动平台,...

枫的等待 3723

junit4功能介绍

 JUnit 是JAVA语言事实上的标准测试库。JUnit 4是三年以来最具里程碑意义的一次发布。它的新特性主要是针对JAVA5中的标记(annotation)来简化测试,而不是利用子类、反射或命名机制。本文将讲述如何使用JUnit 4,当前前提是你最好具有JUnit的使用经验.JUnit, 由Kent Beck 和 Erich Gamma开发,几乎是JAVA开发最重要的第三方工具。正

wfisone的专栏 2772
上一篇: c and c++ static key word
下一篇: API HOOK 金山词霸取词功能原理1
sony315
博客等级 码龄24年 37粉丝 150原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值