实验二JUnit测试框架的使用实验指导 - 副本

本文由用户“我是你的luckly”分享发布 更新时间:2022-12-20 19:44:10 举报文档

以下为《实验二JUnit测试框架的使用实验指导 - 副本》的无排版文字预览,完整格式请下载

下载前请仔细阅读文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

实验二 JUnit测试框架的使用(2学时)

一、实验目的:

通过本次实验,掌握Junit测试框架的使用,会编写测试驱动程序,运行测试用例对Java程序测试。

二、实验内容:

需求:

NextDay是一个简单的日期计算器,计算给定日期的下一天的具体日期。如给定2020年1月1日,返回2020年1月2日;给定2020年1月31日,返回2020年2月1日。主要要求考察学生对日期边界以及异常处理的测试的能力。

要求:

使用JUnit来完成测试,需要将实验步骤截图。

实验总结

通过JUnit测试实验,我学习到了JUnit 作为一个单元测试框架,能够较快地追踪到问题的原因,减少回归错误的纠错难度。 理解了软件测试工具能够提高测试效率......

本实验测试代码如下:代码链接https://blog.csdn.net/qq_***/article/details/***3

本实验测试代码如下:

NextdayTest.java

package net.test;

import static org.junit.Assert.*;

import static org.junit.matchers.JUnitMatchers.containsString;

import net.mooctest.*;

import org.junit.Assert;

import org.junit.Test;

import org.junit.rules.ExpectedException;

public class NextdayTest02 {

// 有效

@Test

public void testDate01() {

Date date = new Date(2,29,2016);

Date d = Nextday.nextDay(date);

String result = d.toString();

Assert.assertEquals("3/1/2016",result);

}

@Test

public void testDate02() {

Date date = new Date(2,28,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

String result = d.toString();

Assert.assertEquals("3/1/2017",result);

}

@Test

public void testDate03() {

Date date = new Date(1,31,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

String result = d.toString();

Assert.assertEquals("2/1/2017",result);

}

@Test

public void testDate04() {

Date date = new Date(4,30,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

String result = d.toString();

Assert.assertEquals("5/1/2017",result);

}

@Test

public void testDate05(){

try{

Date date = new Date(-1,1,1901);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid month"));

}

}

@Test

public void testDate06(){

try{

Date date = new Date(13,1,1901);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid month"));

}

}

@Test

public void testDate07(){

try{

Date date = new Date(1,-1,1901);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate08(){

try{

Date date = new Date(2,30,2016);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate09(){

try{

Date date = new Date(2,29,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate10(){

try{

Date date = new Date(3,32,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate11(){

try{

Date date = new Date(4,31,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate12(){

try{

Date date = new Date(4,32,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate13(){

try{

Date date = new Date(4,32,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate14(){

try{

Date date = new Date(0,3,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid month"));

}

}

@Test

public void testDate15(){

try{

Date date = new Date(4,0,2017);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid day"));

}

}

@Test

public void testDate16(){

try{

Date date = new Date(4,3,0);

Nextday n = new Nextday();

Date d = Nextday.nextDay(date);

}catch (IllegalArgumentException e){

assertThat(e.getMessage(), containsString("Not a valid month"));

}

}

测试程序内容

Year.java

package net.mooctest;

public class Year extends CalendarUnit {

public Year(int pYear) {

setYear(pYear);

}

public void setYear(int pYear) {

setCurrentPos(pYear);

if (!this.isValid()) {

throw new IllegalArgumentException("Not a valid month");

}

}

public int getYear() {

return currentPos;

}

public boolean increment() {

currentPos = currentPos + 1;

if (currentPos == 0)

currentPos = 1;

return true;

}

public boolean isLeap() {

if (currentPos >= 0

&& (((currentPos % 4 == 0) && (currentPos % 100 != 0)) || (currentPos % 400 == 0)))

return true;

else if (currentPos < 0

&& ((((currentPos * -1) % 4 == 1) && ((currentPos * -1) % 100 != 1)) || ((currentPos * -1) % 400 == 1)))

return true;

return false;

}

protected boolean isValid() {

if (this.currentPos != 0)

return true;

return false;

}

public boolean equals(Object o) {

if (o instanceof Year) {

if (this.currentPos == ((Year) o).currentPos)

return true;

}

return false;

}

}

Month.java

package net.mooctest;

public class Month extends CalendarUnit {

private Year y;

private int[] sizeIndex = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

public Month(int pMonth, Year y) {

setMonth(pMonth, y);

}

public void setMonth(int pMonth, Year y) {

setCurrentPos(pMonth);

this.y = y;

if (!this.isValid()) {

throw new IllegalArgumentException("Not a valid month");

}

}

public int getMonth() {

return currentPos;

}

public int getMonthSize() {

if (y.isLeap())

sizeIndex[1] = 29;

else

sizeIndex[1] = 28;

return sizeIndex[currentPos - 1];

}

public boolean increment() {

currentPos += 1;

if (currentPos > 12)

return false;

else

return true;

}

public boolean isValid() {

if (y != null && y.isValid())

if (this.currentPos >= 1 && this.currentPos

以上为《实验二JUnit测试框架的使用实验指导 - 副本》的无排版文字预览,完整格式请下载

下载前请仔细阅读上面文字预览以及下方图片预览。图片预览是什么样的,下载的文档就是什么样的。

图片预览