?????????????????????????????????κο???????????????????????????????????д?????????????Unit Test??????????????????????Panagiotis Louridas????????????е?Java???????OSS????????????????????——JUnit?????顣
??????1947??9??9???????????????Harvard Mark ???п??????????????????????????????????“?????Bug”???????????Smithsonian??????????????????????????Manrice Wilkes????????????????????????????????“???????????μ???????к???????????????????????д??????в??????”????????????????????????????????????
???????????????????bug???????????????????????????????????????????????????????????ЩС?????????????????????????????“????“??????????Σ???????????????д????????????????????????????????bug???????????????????????????????κ??????
????JUnit???????????????Java???????????????????????????????JUnit????????????????????????д???????Kent Beck??Erich Gamma??Beck??SmallTalk???????л??????в???????JUnit?????????????????????????????С??????д????е???????????е?????д????????????????????????????е?bug?????????????????????????????????е??????????????仯???仯????????д??????仯???????????????????????????????????Щ?仯?????????
????????????????????????XP??eXtreme Programming???????????????????????????????????????????У????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Щ????????????к?????????
???????JUnit???
?????????????У??????????д?????????????????????????????????????????????????????????????????????????????????????д???е?if????????????????????б?????JUnit?У?????????????Щif???????д?????assertion?????????????????????????????????????????????????????????б?????????????????????????????
??????????????????????“??????”????????????????????????????JUnit?У????????????????setup????setup??assertions????????????????????????????????????????????setup??????????????С?
?????????????????????????????????????????Щ?????????JUnit?У?????????????ж??TearDown?????????????????????????κε??????????????????????????????????????????setup??????????????С?setup??teardown???????????????????
???????????????????????????Test Cases????????????????????????????С??????????????????????????????????????????????????????????????????Щ??????????С??????????????У??????????????????饗test suites????????????£????????????????????????????????饗test suites?????????????????????????С?
???????????????
??????????????JUnit????????????????????????Complex??????????????????????????????Complex???????????????????????????????????Щ???????в????
???????????????TestCase??????????????У??fixture?????????setup??teardown???????????????????дsetUp()??????????????????????????дtearDown()?????????????в???????????????????????κε?????á???????????У????е????????и??????????????????????дtearDown()??????Figure 1??a??????????δ??????TestCase??????дsetUp()??????
??????a??
????import junit.framework.TestCase;
????import junit.framework.Test;
????import junit.framework.TestSuite;
????public class ComplexTest extends TestCase {
????private Complex a;
????private Complex b;
????protected void setUp() {
????a = new Complex(1?? -1);
????b = new Complex(2?? 5);
????}
????}
??????b??
????public void testComplexEquality()
????Complex expected = new Complex(1?? -1);
????assertEquals(expected?? a);
????}
????public void testComplexAddition() {
????Complex expected = new Complex(3?? 4);
????assertEquals(expected?? a.add(b));
????}
????public void testComplexMultiplication() {
????Complex expected = new Complex(1*2 - (-1)*5??1*5 + (-1)*2);
????assertEquals(expected?? a.multiply(b));
????}
??????c??
????public static Testsuite() {
????return new TestSuite(ComplexTest.class);
????}
????Figure 1 ????????????????a??JUnit?????setup????b??JUnit????????????c???????????????????????????????????饗test suite??
??????????????????????????ComplexTest?????????????????е?assertion??????????????????“test”??????????????public???ε??public ??????????????JUnit???????Java????????????????????????????????????????е????????Complex??????д??java.lang.Object??equals???????????????????
????Figure 1??b???????????????????????????
??????????????κε?Java?????ж??????assertEquals????????The assertEquals method is overloaded and defined for all wuhuif primitives?? apart from wuhuif objects.?????assertEquals??????????д???????????Java?????е???塣?????????assertTrue??assertFalse??????????????assertNull??assertNotNull???ж?????????????assertSame??assertNotSame???????????????????????????á?
?????????????????в?????????????????JUnit???????????????????????????????????????????д?Щ?????????????????????????????????÷??????????????????е????????????????????Figure 1??c???????ComplexTest??????????Testsuite??????????