??????????????????????????????????????????????????????????????????????????????????ú??????????????д???????е????????????????????
???????????????????μ?money??????????????????????????????????????????????testAddMoney101()??testEquals()?????????????ν????????????

1 JUnit
?????????????μ??????μ???Borland JBuilder?????????????????????????????????Java ?汾???????????JUnit??
JUnit???д????????????????????????????е????xUnit????????????????

http://junit.sourceforge.net

http://www.junit.org

????????????????????
?????????????????????????????????????в??????????????TestCase???д???????????????????????????′???

import junit.framework.*
public class TestMoney101 extends TestCase {
public TestMoney101(String s) {
super(s);
}

protected void setUp() {
}

protected void tearDown() {
}

}
 

????TestMoney101?????????constructor??????????????????????????????setUp()??tearDown()??????????????????????????????軌???????????????????κ??????????????????????????Щ???????????????????????????
2 ???????????
????????????????????TestCase ?????????????????????????????????????????′???TestMoney101??


public void testAddMoney101() {
Money101 m12EUR = new Money101(12?? "EUR"); // (1)
Money101 m14EUR = new Money101(14?? "EUR"); // (1)
Money101 expected = new Money101(26?? "EUR");
Money101 result = m12EUR.addMoney101(m14EUR); // (2)
Assert.assertTrue(expected.equals(result)); // (3)
}

???????testAddMoney101() ????
??1???????????????
??2??????????????????????addMoney101()????
??3????????????????????equals()??????У??????Assert.assertTrue()????
?????????????????????Ω??????????????????public?????
?????????????м?????????????????????£?


public void testEquals() {
Money101 m12EUR = new Money101(12?? "EUR");
Money101 m14EUR = new Money101(14?? "EUR");
Assert.assertTrue(!m12EUR.equals(null));
Assert.assertTrue(m12EUR.equals(m12EUR));
Assert.assertTrue(m12EUR.equals(new Money101(12?? "EUR")));
Assert.assertTrue(!m12EUR.equals(m14EUR));
}

???????????Assert ??????????????????????????????????????Assert????????
3 ???????Assert??
?????????????????????????????????磬??C??C++?У?assert()?????????????????????????????????????????????JUnit?У??????????????????е????κ?Assert?????????TestCase ??Assert??????
????C??C++??????assertion ???????Assert??????????????junit.framework.Assert?????е?????????????
Assert.assertEquals
Assert.assertNotNull
Assert.assertNull
Assert.assertSame
Assert.assertTrue
Assert.fail

??fail?????????????????????????????????????????????????????????????????????????????fail?????Assert.fail(String)??Assert.fail()??Assert?fail??
??Assert?????????????????????????????????????????Щ?????е??κ??????????????????
????junit.framework.Assert?е???????????????д????????????????????????TestCase ?????????????????Assert??

public class TestMoney extends TestCase {
Money101 f12EUR;
Money101 f14EUR;
public TestMoney101(String s) {
super(s);
}

protected void setUp() {
// set up the fixture
f12EUR = new Money101(12?? "EUR");
f14EUR = new Money101(14?? "EUR");
}

protected void tearDown() {
}

public void testAddMoney101() {
Money101 expected = new Money101(26?? "EUR");
Money101 result = f12EUR.addMoney101(f14EUR);
Assert.assertTrue(expected.equals(result));
}

public void testEquals() {
Assert.assertTrue(!f12EUR.equals(null));
Assert.assertTrue(f12EUR.equals(f12EUR));
Assert.assertTrue(f12EUR.equals(new Money101(12?? "EUR"));
Assert.assertTrue(!f12EUR.equals(f14EUR));
}

}
 

5 ?????
???????????????????????????????????e??????????е???????????????????
?????????????????TestCase ??????????Test ?????????????????Ω?????????????????????????????????????????????????????????????????????????У???????????????????С??????????????κβ??????????????
???????????в??????????????????????????????????У????????????????????????????????
5.1 ???????????
?????????????????????????????????????????????????????Test??????????????????????????е??????????????????????????????£?

public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new TestMoney101("testEquals"));
suite.addTest(new TestMoney101("testAddMoney"));
return suite;
}
 

suite()???????????Test????????????????????????????????????????????????
???????????????????????????????????????????÷????????????????????????????£?

public static Test suite() {
return new testSuite(TestMoney101.class);

5.2 ???????????
??????t??Test???????????????


public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new TestMoney101("money equals") {
protected void runTest() { testEquals(); }
});
suite.addTest(new TestMoney101("money add") {
protected void runTest() { testAddMoney101(); }
});
return suite;

5.3 JBuilder????
??JBuilder?????Test Runner????Щ????????????????????

 

JBuilder??????3??test runner ??????????test runner ?????????????????檔
JUnit Text UI
JUnit Swing UI
JUnit Awt UI
?????????Project??????Properties???????Run??????Configurations????????Run??壬?????????Test Runner?????Test Runner????Borland JBuilder ????????????С?
?????????????????????С??????????? JBuilder ??????ò???????????????????????????
????????????JBuilder???衷??顣