被测试类:

package project;

public class MyCalendar2 {
public int getNumberOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 6 || month == 7 ||
month == 8 || month == 10 )
return 31;
if (month == 4 || month == 5 || month == 9 || month == 11)
return 30;
if (month == 2) return 28;
return 0; // If month is incorrect
}
}

设计测试数据:

             输入

预计输出

2018  1

31

2018  2

28

2018  3

31

2018  4

30

2018  5

31

2018  6

30

2018  7

31

2018  8

31

2018  9

30

2018  10

31

2018  11

30

2018  12

31

2018  13

0

2008  2

29

  

一般测试:

package project;

import static org.junit.Assert.*;

import org.junit.Test;

/**
*
* @author weiTangzhao
* @Time
*
*/
public class MyCalendar2Test { MyCalendar2 m = new MyCalendar2();
@Test
public void testGetNumberOfDaysInMonth1(){
int days = m.getNumberOfDaysInMonth(2018, 1);
assertEquals(31, days);
} @Test
public void testGetNumberOfDaysInMonth2(){
int days = m.getNumberOfDaysInMonth(2008, 2);
assertEquals(29, days);
} @Test
public void testGetNumberOfDaysInMonth21(){
int days = m.getNumberOfDaysInMonth(2018, 2);
assertEquals(28, days);
}
@Test
public void testGetNumberOfDaysInMonth3(){
int days = m.getNumberOfDaysInMonth(2018, 3);
assertEquals(31, days);
} @Test
public void testGetNumberOfDaysInMonth4(){
int days = m.getNumberOfDaysInMonth(2008, 4);
assertEquals(30, days);
} @Test
public void testGetNumberOfDaysInMonth5(){
int days = m.getNumberOfDaysInMonth(2018, 5);
assertEquals(31, days);
} @Test
public void testGetNumberOfDaysInMonth6(){
int days = m.getNumberOfDaysInMonth(2018, 6);
assertEquals(30, days);
} @Test
public void testGetNumberOfDaysInMonth7(){
int days = m.getNumberOfDaysInMonth(2018, 7);
assertEquals(31, days);
} @Test
public void testGetNumberOfDaysInMonth8(){
int days = m.getNumberOfDaysInMonth(2018,8);
assertEquals(31, days);
}
@Test
public void testGetNumberOfDaysInMonth10(){
int days = m.getNumberOfDaysInMonth(2018, 10);
assertEquals(31, days);
} @Test
public void testGetNumberOfDaysInMonth9(){
int days = m.getNumberOfDaysInMonth(2018, 9);
assertEquals(30, days);
} @Test
public void testGetNumberOfDaysInMonth11(){
int days = m.getNumberOfDaysInMonth(2018, 11);
assertEquals(30, days);
}
@Test
public void testGetNumberOfDaysInMonth12(){
int days = m.getNumberOfDaysInMonth(2018, 12);
assertEquals(31, days);
} @Test
public void testGetNumberOfDaysInMonth13(){
int days = m.getNumberOfDaysInMonth(2018, 13);
assertEquals(0, days);
} }

参数化测试:

package project;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection; import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class)
public class MyCalendar2Test2 { private int input1;
private int input2;
private int expected; /**
* 准备数据。数据的准备需要在一个方法中进行,该方法需要满足一定的要求: 1)该方法必须由Parameters注解修饰
2)该方法必须为public static的
3)该方法必须返回Collection类型
4)该方法的名字不做要求
5)该方法没有参数
* @return
*/
@Parameters
@SuppressWarnings("unchecked")
public static Collection prepareData(){
Object[][] object = {{2018,1,31},{2018,2,28},{2018,3,31},{2018,4,30},{2018,5,31},{2018,6,30},
{2018,7,31},{2018,8,31},{2018,9,30},{2018,10,31},{2018,11,30},{2018,12,31},{2018,13,0},{2008,2,29}}; return Arrays.asList(object);
} public MyCalendar2Test2(int input1,int input2,int expected){
this.input1 = input1;
this.input2 = input2;
this.expected = expected;
} @Test
public void testGetNumberOfDaysInMonth(){
MyCalendar2 m = new MyCalendar2();
int result = m.getNumberOfDaysInMonth(input1,input2);
Assert.assertEquals(expected,result);
} }

参考博客:

https://www.cnblogs.com/byron0918/p/4801152.html

测试 | 单元测试工具 | JUnit | 参数化的更多相关文章

  1. 测试 | 单元测试工具 | JUnit

    http://junit.sourceforge.net/javadoc/org/junit/Assert.html 使用: 新建测试类: 在预测试的类上点击右键--->NEW--->Ju ...

  2. 单元测试工具Junit浅谈

    什么是单元测试?   写了一个类和一些方法,给别人用,会不会有bug?那就测一下这些方法吧 怎么测?   用main方法测?不能一起运行,需要人为观察输出是否正确,测试效率低 单元测试能带来什么好处? ...

  3. Maven的安装配置及初次创建项目与java单元测试工具JUnit

    Maven  安装     1.把maven安装包解压到某个位置     2.配置M2_HOME环境变量指向这个位置 3.在path环境变量中添加;%M2_HOME%\bin 配置镜像 国内的阿里云镜 ...

  4. 11th 单元测试工具JUnit的学习

    1.写好一个简易的四则运算的程序 UnitTest类文件: public class UnitTest { int a; int b; int answer;//正确答案 public int plu ...

  5. Hibernate单元测试工具junit

    相关注解 @Text :测试方法 @Before :初始化方法 @After : 释放资源

  6. 单元测试利器 JUnit 4

    引言 毋庸置疑,程序员要对自己编写的代码负责,您不仅要保证它能通过编译,正常地运行,而且要满足需求和设计预期的效果.单元测试正是验证代码行为是否满足预期的有效手段之一.但不可否认,做测试是件很枯燥无趣 ...

  7. [转]单元测试利器 JUnit 4

    引言 毋庸置疑,程序员要对自己编写的代码负责,您不仅要保证它能通过编译,正常地运行,而且要满足需求和设计预期的效果.单元测试正是验证代码行为是否满足预期的有效手段之一.但不可否认,做测试是件很枯燥无趣 ...

  8. Java知识积累——单元测试和JUnit(一)

    说起单元测试,刚毕业或者没毕业的人可能大多停留在课本讲述的定义阶段,至于具体是怎么定义的,估计也不会有太多人记得.我们的教育总是这样让人“欣 慰”.那么什么是单元测试呢?具体科学的定义咱就不去关心了, ...

  9. 单元测试实战 - Junit测试

    一.对加法函数进行测试 1.实例化被测单元(方法):类名 实例名=new 类名([参数]) 2.调用被测单元,对比预期值和输出值(实际值): 在没有junit测试工具的情况下,我们要进行如下的测试代码 ...

随机推荐

  1. 分词系统简介:PHPAnalysis分词程序

    分词系统简介:PHPAnalysis分词程序使用居于unicode的词库,使用反向匹配模式分词,理论上兼容编码更广泛,并且对utf-8编码尤为方便. 由于PHPAnalysis是无组件的系统,因此速度 ...

  2. const位置上的不同代表哪些不同的意义

    const位置上的不同代表哪些不同的意义 exern的用法 这个可以引用在程序编译过程中编译进去的常量数据.换句话说正能在.h文件的声明赋值的常量才可以.并且常量的名字不能相同,如果相同会报错. 全局 ...

  3. 转载的C#学习笔记

    转载地址:http://www.cnblogs.com/renyanlei/p/4075065.html 最近在一个培训机构里面教授Net知识.每天都会带领学生学习c#知识.我希望把每天学习的笔记记录 ...

  4. Codeforces Round #401 (Div. 2) C Alyona and Spreadsheet —— 打表

    题目链接:http://codeforces.com/contest/777/problem/C C. Alyona and Spreadsheet time limit per test 1 sec ...

  5. VS2010关于调用ffmpeg借口出错

    win7 下开发视频服务器,用到ffmpeg,debug版本运行正常,切换到release时,出现"0x00905a4d 处未处理的异常: 0xC0000005: 读取位置 0x00905a ...

  6. 对服务器上所有Word文件做全文检索的解决方案-Java

    一.背景介绍    Word文档与日常办公密不可分,在实际应用中,当某一文档服务器中有很多Word文档,假如有成千上万个文档时,用户查找打开包含某些指定关键字的文档就变得很困难,目前这一问题没有好的解 ...

  7. Win32环境下代码注入与API钩子的实现

    本文详细的介绍了在Visual Studio(以下简称VS)下实现API钩子的编程方法,阅读本文需要基础:有操作系统的基本知识(进程管理,内存管理),会在VS下编写和调试Win32应用程序和动态链接库 ...

  8. 配置哨兵监控Redis运行情况

    Redis的主从架构,如果master发现故障了,还得手动将slave切换成master继续服务,手动的方式容易造成失误,导致数据丢失,那Redis有没有一种机制可以在master和slave进行监控 ...

  9. 在项目中添加全局的 pch 文件

    说明,本片博文仅仅是方便自己以后在添加 pch 文件的配置时候参照使用,担心一些配置的路径由于时间而遗忘. (1)建一个 pch 文件 注意下面要 在 Targets 后打上 对号 (2)对该文件进行 ...

  10. SQL 系统表

    http://www.cnblogs.com/asdcer/archive/2007/05/14/746377.aspx