# 随笔档案(123)

•

• 积分 - 152312
• 排名 - 391

### 最新评论

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

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

/**
* JUnit4.5 QuickTutorial
*
* @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 ; }
}

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);
}

}

[参考资料]: