JUnit???????????????????????JUnit3.8??
???????????? ???????[ 2015/8/26 10:31:00 ] ??????????????? Junit ???????
??????????????з????????
??????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()?????????????????????????????μ?????????????????и????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11