被测试类:

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. 【LeetCode】Maximum Depth of Binary Tree

    http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...

  2. sudo出现unable to resolve host

    是因为/etc/hosts下的主机名和/etc/hostname下的主机名不一致所导致的错误,将两个改为一致即可

  3. vsftp登录时间太长的解决办法

    与ssh一样,vsftp的配置文件默认开启了DNS反向解析,这可能会造成用户在登陆到FTP服务器的时候奇慢无比,只要在配置文件中禁用DNS反向解析即可解决文件. 编辑/etc/vsftpd/vsftp ...

  4. break和continue 都是指的最接近的内层循环

    break和continue 都是指的最接近的内层循环

  5. IPFS - 可快速索引的版本化的点对点文件系统(草稿3)

    摘要 星际文件系统是一种点对点的分布式文件系统, 旨在连接所有有相同的文件系统的计算机设备.在某些方面, IPFS类似于web, 但web 是中心化的,而IPFS是一个单一的Bittorrent 群集 ...

  6. linux文件查找(find,locate)

    文件查找: locate:       非实时,模糊匹配,查找是根据全系统文件数据库进行的: # updatedb, 手动生成文件数据库 速度快   find:       实时       精确   ...

  7. P4147玉蟾宫——最大子矩阵

    悬线法裸题. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace ...

  8. Android开发---开发文档翻译

    2014.11.24 1:ClipData类:用于表示剪切的数据,此剪切的数据可以是复杂类型,包括一个或多个条目实例 (1)基础知识 >公共类:public class >嵌套类:Clip ...

  9. 1.1-1.5 flume架构概述及安装使用

    一.flume架构概述 1.flume简介 Flume是一种分布式,可靠且可用的服务,用于有效地收集,聚合和移动大量日志数据.它具有基于流数据流的简单灵活的架构.它具有可靠的可靠性机制和许多故障转移和 ...

  10. Flutter实战视频-移动电商-05.Dio基础_引入和简单的Get请求

    05.Dio基础_引入和简单的Get请求 博客地址: https://jspang.com/post/FlutterShop.html#toc-4c7 第三方的http请求库叫做Dio https:/ ...