??????????????з????????
??????JUnit 3.8?У??????????????????????
????1.public???
????2.void???
????3.???????????
????4.?????????????test????? ?????????????????з?????????????test????????????
????Test Case????????????????????????????????κε??????????
????????Щ???????????????????????κε????
???????????????????????????????
???????????д???????£?
package com.mengdd.junit;
import junit.framework.Assert;
import junit.framework.TestCase;
public class CalculatorTest extends TestCase
{
public void testAdd()
{
Calculator calculator = new Calculator();
int result = calculator.add(1?? 2);
// ?ж???????????
Assert.assertEquals(3?? result);// ????????????????????????????????????
}
public void testSubtract()
{
Calculator calculator = new Calculator();
int result = calculator.subtract(1?? 2);
// ?ж???????????
Assert.assertEquals(-1?? result);// ????????????????????????????????????
}
public void testMultiply()
{
Calculator calculator = new Calculator();
int result = calculator.multiply(2?? 3);
// ?ж???????????
Assert.assertEquals(6?? result);// ????????????????????????????????????
}
public void testDivide()
{
Calculator calculator = new Calculator();
int result = calculator.divide(12?? 3);
// ?ж???????????
Assert.assertEquals(4?? result);// ????????????????????????????????????
}
}
??????????£??????????Run As->JUnit Test

????????????????????Run??????У?
????JUnit?????Keep the bar green to keep the code clean.
????5.?????????setUp()?????????
????????????DRY??Don’t Repeat Yourself??
?????????????????????????????????????????setUp()?????С?
????????д?????protected???public????е????????????Χ?????????????
?????????????????????????
??????CalculatorTest???м?????????£?
????@Override
????public void setUp() throws Exception
????{
????System.out.println("set up");
????}
????@Override
????public void tearDown() throws Exception
????{
????System.out.println("tear down");
????}
??????????к???Console????????£?
???????????????????????Ρ?
??????????????????????setUp()???????????????????tearDown()????С?
????????????????????????????????
????1.setUp()
????2.testXXX()
????3.tearDown()
???????????ó????????????????????????????ж????????????????????????setUp()?У??????????????????????????????μ????
???????????????£?
package com.mengdd.junit;
import junit.framework.Assert;
import junit.framework.TestCase;
public class CalculatorTest extends TestCase
{
private Calculator calculator = null;
@Override
public void setUp() throws Exception
{
System.out.println("set up");
// ???????????????
calculator = new Calculator();
System.out.println(calculator);
}
@Override
public void tearDown() throws Exception
{
System.out.println("tear down");
}
public void testAdd()
{
int result = calculator.add(1?? 2);
// ?ж???????????
Assert.assertEquals(3?? result);// ????????????????????????????????????
}
public void testSubtract()
{
int result = calculator.subtract(1?? 2);
// ?ж???????????
Assert.assertEquals(-1?? result);// ????????????????????????????????????
}
public void testMultiply()
{
int result = calculator.multiply(2?? 3);
// ?ж???????????
Assert.assertEquals(6?? result);// ????????????????????????????????????
}
public void testDivide()
{
int result = calculator.divide(12?? 3);
// ?ж???????????
Assert.assertEquals(4?? result);// ????????????????????????????????????
}
}
???????к??????????
??????????????????????????setUp()??tearDown()?????????????????????????????μ?????????????????и????