第二天junit测试实例

NextDay源代码

package yanhongyun;


public class NextDay 
{
    private int year;
    private int month;
    private int day;
    
    public NextDay()
    {
    }
    
    public NextDay(int year,int month,int day)
    {
      set(year,month,day);
    }
    
    public void set(int year,int month,int day)
    {
       if (year<0)
         this.year=0;
       else
         this.year=year;
       if (month<1 || month>12)
          this.month=1;
       else
          this.month=month;
       if (day<1 || day>lastDay(year,month))
          this.day=1;         
       else
         this.day=day;
    }
    
    private int lastDay(int year,int month)
    {
       if (month==1 || month==3 || month==5 ||month==7 || month==8 || month==10||month==12)
       {
          return 31;
       }
       else if (month==2)
       {
          if ((year%4==0 && year%100!=0) || year %400==0)
            return 29;
          else 
            return 28;
       }
       else
       {
          return 30;
       }
    }
    
    
    public NextDay  nextDay()
    {
       day=day+1;
       if (day>lastDay(year,month))
       {
          day=1;
          month=month+1;         
          if (month>12)
          {
             month=1;
             year=year+1;
          }
       }          
       return this;  
    }
    public boolean equals(Object obj)
    {
       if (obj instanceof NextDay)
       {
          NextDay nd=(NextDay)obj;
          return (nd.year==this.year && nd.month==this.month && nd.day==this.day);
       }
       else
         return false;
       
    }
    public String tndString()
    {
       String str=year+"年"+month+"月"+day+"日";
       return str;
    }
}

Junit 测试程序


package yanhongyun;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TestNextDay extends TestCase
{
   NextDay date1;
   NextDay date2;
   public TestNextDay(String name)
   {
      super(name);      
   }
   public void setUp()
   {
   }
   public void testSet()
   {
      date1=new NextDay(2000,2,30);
      date2=new NextDay(2000,2,1);

      Assert.assertEquals(date2,date1);
      
   date1.set(1969,8,29);
      assertEquals(date1,date1);
        
      date1.set(1991,2,29);
      assertEquals(new NextDay(1991,2,1),date1);
      
   date1.set(-120,2,1);
   assertEquals(new NextDay(0,2,1),date1);

   date1.set(-120,-2,-1);
   assertEquals(new NextDay(0,1,1),date1);


   }
   public void testNextDay()
   {
      date1=new NextDay(1992,12,31);
      date2=new NextDay(1993,1,1);
      
     
      assertEquals(date2,date1.nextDay());
      
      date1.set(1992,2,28);
      
      date2.set(1992,2,29);
      
      assertEquals(date2,date1.nextDay());
      
      date1.set(1992,2,29);
      date2.set(1992,3,1);
      assertEquals(date2,date1.nextDay());
      
      date1.set(1998,2,28);
      date2.set(1998,3,1);
      assertEquals(date2,date1.nextDay());
      
      date1.set(1995,1,19);
      date2.set(1995,1,20);
      assertEquals(date2,date1.nextDay());
      
      date1.set(2006,1,31);
      date2.set(2006,2,1);
      assertEquals(date2,date1.nextDay());
      
      date1.set(2006,4,30);
      date2.set(2006,5,1);
      assertEquals(date2,date1.nextDay());
      
   }
   public static void main(String []args)
   {
      junit.textui.TestRunner.run(TestNextDay.class);
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值