JUnit4 experience

JUnit4 Version inheritates the annotation features from java5.0v,so it simplized many workload for unit test.
Now I am using the feature to speedup my test work.
1.Create your class that will be tested.
package com.aaron;

public class Calculator {
//  static variant for store result
    private static int result;
    public void add(int n){
        result = result + n;
    }
    public void substract(int n){
        //Bug: correctly method for: result =result-n
        result = result - 1;
    }
    public void multiply(int n){
        //TODO
    }        
    public void divide(int n){
        result = result / n;
    }
    public void square(int n){
        result = n * n;
    }
    public void squareRoot(int n){
        //Bug :Dead loop
        for (; ;) ;          
    }
    public void clear(){    
       //clear the result,due to the Fixture feature in junit.
        result = 0;
    }
    public int getResult(){
        return result;
    }
}

Next create the testcase for the class
package com.aaron;
import static org.junit.Assert.*;
import static org.junit.Assert.*;
then you can directly use the assertEquals() method in the testcase.
2.Test class declare,you know that,no specail class extends from junit,you can define the test class with your idea.
3.Create the test object.as the above:private static Calculator calculator = new Calculator();
4.Test methods declare,
you can use some marks for the test methods.such as @Test,@Ignore,@Before,@After.
5.Write your test method,please kindly remaind that no return value in the test method.
6.Fixture utility,
@Before
public 
void setUp() throws Exception{
     calculator.clear();
}

That means if you want to the test methods independent,so you need  initialize the test class when you test a method,so in junit4v, you just declare @Before in the setUp() method,and every time you refer to the class instance with new.
7.High level Fixture:
if you want to test a class which read/write a big block file in every method. you know it's very heavy to read/write a big block file every time.you had better read the file once,then every method can read/write the file by the initialization. in junit,you can use @BeforeClass and @AfterClass to finish that.
and Only one method can be declared public and static.
8.Time test,
 @Test(timeout = 1000),if you have a dead loop to test,how do you control that? the best way is using timeout.(microsecond).
9.Exception handle.
@Test(expected = ArithmeticException.class),if you divide a zero,then? so you need an exception to catch it.
10.Runner.
In my test code,you do not know which runner used,also there are many runners in the Junit framework,Now
you can explicit to declare the runner before the class which you want with
@RunWith(TestClassRunner.class)
11.Parameter
package com.aaron;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class CalculatorTest {

 private static Calculator calculator = new Calculator();
 @Before
 public void setUp() throws Exception {
  calculator.clear();
 }

 @Test
 public void testAdd() {
  calculator.add(2);
        calculator.add(3);
        assertEquals(5, calculator.getResult());
 }

 @Test
 public void testSubstract() {
  calculator.add(10);
        calculator.substract(2);
        assertEquals(8, calculator.getResult());
 }
    @Ignore("Not implement")
 @Test
 public void testMultiply() {
  fail("Not yet implemented");
 }

 @Test
 public void testDivide() {
   calculator.add(8);
      calculator.divide(2);
      assertEquals(4, calculator.getResult());
 }

}

Run the testcase,that will one error occurance.

And above is the basic test process.
Now let's take some more introduction for the JUnit4
1.import the necessary package

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

 @RunWith(Parameterized.class)
 public class ParameterTest{
     private static Calculator calculator = new Calculator();
     private int param;
     private int result;
    
     @Parameters
     public static Collection data(){
         return Arrays.asList(new Object[][]{
                 {2, 4},
                 {0, 0},
                 {-2, 9},
         });
     }

     //Constructor,reinitialize the parameters
     public ParameterTest(int param, int result){
         this.param = param;
         this.result = result;
     }

     @Test
     public void square(){
         calculator.square(param);
         assertEquals(result, calculator.getResult());
     }

 }

12.Package multi-test,due to many test classes for a class,so one by one to test,it's not convenient for user,so in Junit,you can use suite to package all tests for you.
package com.aaron;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        CalculatorTest.class,
        ParameterTest.class
        })
public class AllCalculatorTests{
}

OK,that's all,enjoy that!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值