实验三 Junit单元测试
下面是使用位组来跟踪一年中的那些天是节假日的程序。
首先,假期相应代码如下:
import java.util.*;
public class HolidaySked {
BitSet sked;
public static void main(String[] arguments){
HolidaySked cal=new HolidaySked();
if(arguments.length>0){
try{
int whichDay=Integer.parseInt(arguments[0]);
if(cal.isHoliday(whichDay)){
System.out.println(whichDay+"is a holiday.");}
else
{System.out.println(whichDay+"is not a holiday.");}
}catch(NumberFormatException nfe){
System.out.println("Error: "+nfe.getMessage());}
}
}
public void addHoliday(int daytoAdd) {
sked.set(daytoAdd);
}
public boolean isHoliday(int dayToCheck){
boolean result=sked.get(dayToCheck);
return result;
}
public HolidaySked(){
sked=new BitSet(365);
int[] holiday={1,20,43,48,53,115,131,146,165,166,185,244,286,315,327,359};
for(int i=0;i<holiday.length;i++){
addHoliday(holiday[i]);
}
}
(1) 请用TestCase方法对程序中的isHoliday()方法进行Junit测试;
相应的代码HolidaySkedTest.java为:
package one;
import static org.junit.Assert.*;
import org.junit.Test;
public class HolidaySkedTest {
@Test
public void testisHoliday(){
HolidaySked holiday=new HolidaySked();
assertTrue(holiday.isHoliday(100));
}
}
(2) 用参数化的方法重新设计测试用例。
相应的代码HolidaySkedTestParameter.java为:
package one;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.runner.*;
import org.junit.Test;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class HolidaySkedTestParameter {
private int D;
private boolean result;
@Parameters
public static Collection data(){
Object [][]object={{1,true},{20,true},{43,false},{100,true}};
return Arrays.asList(object);
}
public HolidaySkedTestParameter(int D,boolean result){
this.D=D;
this.result=result;
}
@Test
public void testholiday(){
HolidaySked holiday=new HolidaySked();
assertEquals(result,holiday.isHoliday(D));
}
}
(3) 再用Suite方法对上述两种测试用例进行套件测试。
相应的代码为:
package one;
import org.junit.runner.RunWith;
import org.junit.runner.*;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(value=org.junit.runners.Suite.class)
@SuiteClasses(value = {HolidaySkedTestParameter.class,HolidaySkedTest.class})
public class HolidaySkedTestsuite {
}
对变量J的计算进行自动化测试。测试用例的设计要分别满足语句覆盖、判定-条件覆盖、条件组合、路径覆盖及基本路径测试等不同的测试标准,并利用Eclemma对其进行覆盖率分析
package two;
public class Dowork {
/**
* @param args
*/
public int Work(int x,int y,int z)
{
int k=0,j=0;
if((x>3)&&(z<10))
{
k=x*y-1; //语句块1
j=(int)Math.sqrt(k);
}
if((x==4)||(y>5))
{
j=x*y+10; //语句块2
}
j=j%3;
return j;//语句块3
}
}
测试代码
DoworkTest1.java
package two;
import static org.junit.Assert.*;
import org.junit.Test;
public class DoworkTest1 {
@Test
public void testWork(){
Dowork work=new Dowork();
assertEquals(0,work.Work(5,10,9));
}
}
源程序覆盖情况如图所示:
测试程序覆盖情况如图所示:
Coverage视图如图所示:
HTML报告如图所示:
测试代码:
DoworkTest2.java
package two;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class DoworkTest2 {
private Dowork work1;
private Dowork work2;
@Before
public void setUp() throws Exception {//初始化公用对象
work1= new Dowork();
work2= new Dowork();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testWork(){
assertEquals(work1.Work(4,6,5),1);
assertEquals(work2.Work(2,5,11),0);
}
}
源程序覆盖情况如图所示:
}
测试程序覆盖情况如图所示:
Coverage视图如图所示:
HTML报告如图所示:
测试代码:
DoworkTest3.java
package two;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class DoworkTest3 {
private Dowork work1;
private Dowork work2;
private Dowork work3;
private Dowork work4;
@Before
public void setUp() throws Exception {//初始化公用对象
work1= new Dowork();
work2= new Dowork();
work3= new Dowork();
work4= new Dowork();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testWork(){
assertEquals(work1.Work(4,6,5),1);
assertEquals(work2.Work(4,5,15),0);
assertEquals(work3.Work(2,6,5),1);
assertEquals(work4.Work(2,5,15),0);
}
}
源程序覆盖情况如图所示:
测试程序覆盖情况如图所示:
Coverage视图如图所示:
HTML报告如图所示:
测试代码:
DoworkTest4.java
package two;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class DoworkTest4 {
private Dowork work1;
private Dowork work2;
private Dowork work3;
private Dowork work4;
@Before
public void setUp() throws Exception {//初始化公用对象
work1= new Dowork();
work2= new Dowork();
work3= new Dowork();
work4= new Dowork();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testWork(){
assertEquals(work1.Work(4,6,5),1);
}
}
源程序覆盖情况如图所示:
测试程序覆盖情况如图所示:
Coverage视图如图所示:
HTML报告如图所示:
首先是先分析程序流图,然后分析程序圈复杂度,从而确定出独立路径的条数应该为5,并写出相应的测试用例;
测试代码:
DoworkTest5.java
package two;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class DoworkTest5 {
private Dowork work1;
private Dowork work2;
private Dowork work3;
private Dowork work4;
private Dowork work5;
@Before
public void setUp() throws Exception {//初始化公用对象
work1= new Dowork();
work2= new Dowork();
work3= new Dowork();
work4= new Dowork();
work5= new Dowork();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testWork(){
assertEquals(work1.Work(4,6,5),1);
assertEquals(work2.Work(5,6,5),1);
assertEquals(work3.Work(5,5,5),1);
assertEquals(work4.Work(4,5,15),0);
assertEquals(work5.Work(2,6,15),1);
}
}
源程序覆盖情况如图所示:
测试程序覆盖情况如图所示:
Coverage视图如图所示:
HTML报告如图所示:
综合练习:结合自己的Java编程经历,选择一个你自己曾经做过的java程序,尽可能多的运用所学过的Junit单元测试和Eclemma覆盖率测试方法帮助你进行代码的测试,并形成详细的报告。
Sum.java
package three;
public class sum {
private int sum;
public sum(){
this.sum=0;
}
public int add(int a,int b){
if(a>0||b<0){
sum+=a;
}
if(a<20&&b>5){
sum-=b;
}
if(sum>0&∑<10)
return sum;
else
return 0;
}
}
package three;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import two.Dowork;
public class sumTest1 {
private sum sum1;
@Before
public void setUp() throws Exception {//初始化公用对象
sum1= new sum();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testadd(){
assertEquals(sum1.add(17,6),0);
}
}
源程序覆盖情况如图所示:
测试程序覆盖情况如图所示:
Coverage视图如图所示:
HTML报告如图所示:
package three;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class sumTest2 {
private sum sum1;
private sum sum2;
@Before
public void setUp() throws Exception {//初始化公用对象
sum1= new sum();
sum2= new sum();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testadd(){
assertEquals(sum1.add(-1,0),0);
assertEquals(sum1.add(9,6),3);
}
}
源程序覆盖情况如图所示:
测试程序覆盖情况如图所示:
Coverage视图如图所示:
HTML报告如图所示:
package three;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import two.Dowork;
public class sumTest3 {
private sum sum1;
private sum sum2;
@Before
public void setUp() throws Exception {//初始化公用对象
sum1= new sum();
sum2= new sum();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testadd(){
assertEquals(sum1.add(6,-1),6);
assertEquals(sum2.add(5,6),0);
}
}
源程序覆盖情况如图所示:
测试程序覆盖情况如图所示:
HTML报告如图所示:
合并多次测试覆盖率