[转]JUnit4.5 QuickTutorial

[标题]:[转]JUnit4.5 QuickTutorial
[时间]:2009-7-5
[摘要]:JUnit官方例子QuickTutorial
[关键字]:JUnit、Test、测试、单元测试、helloworld
[环境]:JUnit4.5、MyEclipse7
[作者]:Winty ([email protected]) http://www.blogjava.net/wintys

[正文]:
Subscription.java:
package wintys.junit;

/**
 * JUnit4.5 QuickTutorial
 * http://code.google.com/p/t2framework/wiki/JUnitQuickTutorial
 *  
 * @author Winty
 * @version 2009-07-04
 */
public class Subscription {
       private int price ; // subscription total price in euro-cent
       private int length ; // length of subscription in months

       // constructor :
       public Subscription(int p, int n) {
         price = p ;
         length = n ;
       }

      /**
       * Calculate the monthly subscription price in euro,
       * rounded up to the nearest cent.
       */
       public double pricePerMonth() {
         double r = (double) price / (double) length /100.0;
          return r ;
       }

      /**
       * Call this to cancel/nulify this subscription.
       */
       public void cancel() { length = 0 ; }
}

用JUnit测试:
SubscriptionTest.java:
package wintys.junit;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SubscriptionTest {
    Subscription S;

    @Before
    public void setUp() throws Exception {
         S = new Subscription(200,2) ;
    }

    @After
    public void tearDown() throws Exception {
        S = null;
    }

    @Test
    public void testPricePerMonth() {
        
        assertEquals(S.pricePerMonth() , 1.0);
    }

}

[参考资料]:
JUnitQuickTutorial : http://code.google.com/p/t2framework/wiki/JUnitQuickTutorial

你可能感兴趣的:(Google,JUnit,单元测试,UP,Gmail)