登录慕测平台从慕测开发者测试练习的Triangle和NextDay程序中分别生成满足100%语句覆盖、函数覆盖

生成JUnit测试用例集
1.对Triangle进行JUnit测试并设计测试用例使其满足100%函数覆盖
2.对Triangle进行JUnit测试并设计测试用例使其满足100%语句覆盖
3.对Triangle进行JUnit测试并设计测试用例使其满足100%判定覆盖
4.对NextDay进行JUnit测试并设计测试用例使其满足100%函数覆盖
5.对NextDay进行JUnit测试并设计测试用例使其满足100%语句覆盖
6.对NextDay进行JUnit测试并设计测试用例使其满足100%判定覆盖


#Triangle
package net.mooctest;
 import static org.junit.Assert.*;
 import org.junit.Test;
 public class TriangleTest {
  
  Triangle T1 = new Triangle(4, 6, 10);
  Triangle T2 = new Triangle(6, 10, 8);
  Triangle T3 = new Triangle(10, 10, 10);
  Triangle T4 = new Triangle(0, 10, 10);
  Triangle T5 = new Triangle(10, 10, 13);
  
  @Test
  public void testIsTriangle() {
   assertEquals(false, T1.isTriangle(T1));
  }
 @Test
  public void testIsTriangle2() {
   assertEquals(true, T2.isTriangle(T2));
    }
  @Test
  public void testgetType() {
   assertEquals("Scalene", T2.getType(T2));
  }
  @Test
  public void testgetType1() {
   assertEquals("Regular", T3.getType(T3));
  }
  @Test
  public void testgetType2() {
   assertEquals("Illegal", T4.getType(T4));
  }
  @Test
  public void testgetType3() {
   assertEquals("Isosceles", T5.getType(T5));
     }
  @Test
  public void testgetBorders() {
   assertEquals(3, T2.getBorders().length);
  }
 }
 #nextday
  package net.mooctest;
 import static org.junit.Assert.*;
 import org.junit.Test;
 public class NextdayTest {
  @Test
  public void testValid() {
   //rmal day++
   Date currentday=new Date(12,30,2019);
   Date nextday=Nextday.nextDay(currentday);
   Date expectday=new Date(12,31,2019);
   assertEquals(expectday,nextday);
   //rmal day->next month
   currentday=new Date(11,30,2019);  
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(12,1,2019);
   assertEquals(expectday,nextday);
   //rmal month->next year
   currentday=new Date(12,31,2019);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(1,1,2020);
   assertEquals(expectday,nextday);
   //rmal leap year 2016, Feb 28
   currentday=new Date(2,28,2016);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(2,29,2016);
   assertEquals(expectday,nextday);
   //special leap year 2012, Feb 28
   currentday=new Date(2,28,2012);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(2,29,2012);
   assertEquals(expectday,nextday);
   currentday=new Date(2,27,2012);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(2,28,2012);
   assertEquals(expectday,nextday);
   //special leap year -2017, Feb 28
   currentday=new Date(2,28,-2017);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(2,29,-2017);
   assertEquals(expectday,nextday);
   //special leap year -2001, Feb 28
    currentday=new Date(2,28,-2001);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(2,29,-2001);
   assertEquals(expectday,nextday);
   //special leap year -2018, Feb 28
   currentday=new Date(2,28,-2018);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(3,1,-2018);
   assertEquals(expectday,nextday);
   //special leap year -1, Dec 31
   currentday=new Date(12,31,-1);
   nextday=Nextday.nextDay(currentday);
   expectday=new Date(1,1,1);
   assertEquals(expectday,nextday); //method coverage
   currentday.printDate();
   currentday.toString();
   
   //function equals() coverage for Date, Day, Month, and Year
   assertEquals(31,currentday.getDay().getDay());
   assertEquals(12,currentday.getMonth().getMonth());
   assertEquals(-1,currentday.getYear().getYear());
   Year y1=new Year(1111);
   Month m1=new Month(11,y1);
   Day d1=new Day(20,m1);
   assertEquals(false,currentday.getDay().equals(d1));
   assertEquals(false,currentday.getMonth().equals(m1));
   assertEquals(false,currentday.getYear().equals(y1));
   
   assertEquals(false,currentday.getYear().equals(m1));
   assertEquals(false,currentday.getMonth().equals(y1));
   assertEquals(false,currentday.getDay().equals(m1));
   assertEquals(false,currentday.equals(y1));
   y1=new Year(-1);
   m1=new Month(12,y1);
   d1=new Day(31,m1);
   assertEquals(true,currentday.getDay().equals(d1));
   assertEquals(true,currentday.getMonth().equals(m1));
   assertEquals(true,currentday.getYear().equals(y1));  currentday=new Date(1,1,2019);
   nextday=new Date(1,1,2019);
   assertEquals(true,currentday.equals(nextday));
   nextday=new Date(1,2,2019);
   assertEquals(false,currentday.equals(nextday));
  }
  @Test(expected=IllegalArgumentException.class)
      public void testInvalidDay() {
      //day>31 exception
   Year y=new Year(2019);
   Month m=new Month(11,y);
   Day d=new Day(32,m);
  }
  @Test(expected=IllegalArgumentException.class)
      public void testInvalidDay1() {
   Day d=new Day(2,null); }
  @Test(expected=IllegalArgumentException.class)
      public void testInvalidMonth1() {
   //Month>12 exception
   Year y=new Year(2019);
   Month m=new Month(13,y);  
  }
  @Test(expected=IllegalArgumentException.class)
      public void testInvalidMonth2() {
   //Month>13 exception
   Year y=null;
   Month m=new Month(1,y);  
  }
  @Test(expected=IllegalArgumentException.class)
      public void testInvalidYear() {
      //year=0 exception
        Year y=new Year(0);   }}

你可能感兴趣的:(单元测试,java,测试工具,测试用例)