被测试类:

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. MySQL 的“root”用户修改密码

    MySQL 的“root”用户默认状态是没有密码的,所以在 PHP 中您可以使用 mysql_connect("localhost","root"," ...

  2. KeyChain相关参数的说明

    #pragma mark- 密钥类型 //密钥类型键 //CFTypeRef kSecClass // //值 //CFTypeRef kSecClassGenericPassword         ...

  3. vuejs实现折叠面板展开收缩动画

    vuejs通过css3实现元素固定高度到auto高度的动画和auto高度到固定高度的动画. 循环列表,html: <template> <div class="newsli ...

  4. 报错:'Navigator is deprecated and has been removed from this package. It can now be installed

    报错:'Navigator is deprecated and has been removed from this package. It can now be installed ' +     ...

  5. powershell 扩展 (PSCX) 安装指南

    在玩ansible的过程中,使用win_unzip模块时powershell支持不了,需要安装PSCX对powershell进行扩展,随手记录下安装过程. 从官网下载的Pscx是一个zip压缩文件,解 ...

  6. SQL server 备份/恢复/压缩 进度查询

    第一步,用 sp_who2 查出备份的sid(或在窗口中的连接属性中看) exec sp_who2 第二步,用以下查询获得运行情况(看 percent_complete列) SELECT sessio ...

  7. 关于Froala Editor的简单使用

    1.添加样式表 <!-- 核心样式表 --> <link rel="stylesheet" href="${ctx}/resources/froala_ ...

  8. Tensorflow基础知识

    基本知识 使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使 ...

  9. HDU3065(AC自动机入门题)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  10. CSS:CSS 选择器参考手册

    ylbtech-CSS:CSS 选择器参考手册 1.返回顶部 1. 我们会定期对 W3School 的 CSS 参考手册进行浏览器测试. CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需 ...