天天看點

JUnit 注解@RunWith的工作原理

In order to introduce the usage of this annotation in JUnit, I use an example to demonstrate.

I have a very simple price calculator:

JUnit 注解@RunWith的工作原理
Test class for this calculator:
JUnit 注解@RunWith的工作原理

The disadvantage of this solution: here I have two sets of test data, so duplicate static attribute QUANTITY2, PRICE2, DISCOUNT2 and EXPECTED2 are introduced, which is a violation of DRY – Don’t Repeat Yourself.

A better solution

import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
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 RunWithTwoTestCase {
    private int quantity;
    private double price;
    private double discount;
    private double expected; 
    private PriceCalculator priceCalculator;
    
    public RunWithTwoTestCase(int qty, double price, double discount, double expected){
        this.quantity = qty;
        this.price = price;
        this.discount = discount;
        this.expected = expected;
    }
    
    @Parameters(name = "{index}: (Quantity {0} * Price {1}) * (Discount{2}/100) = {3}")
    public static Collection<Object[]> generateData()
    {
        return Arrays.asList(new Object[][] {
                              { 5, 10, 90, 45 },
                              { 4, 5, 80, 16 } });
    }
    
    @Before
    public void setUp() throws Exception {      
        this.priceCalculator = new PriceCalculator(this.quantity, this.price, this.discount);
    }
    
    @Test
    public void testPrice(){
        assertEquals("price calculated for test data", this.expected, 
                this.priceCalculator.getPrice(), 0);
    }
}
      
JUnit 注解@RunWith的工作原理

The advantage of this solution is, the test data is in fact somehow separated from test class itself. In case you need more test data, you can simply append array in method generateData() without any duplicate static attributes.

Another benefit is, the test data injected with @Parameters are also available in JUnit result view, which is easier for tester to analyze result. Just compare the normal test case and the solution where @Parameters is used:

JUnit 注解@RunWith的工作原理

In the runtime,

(1) the annotation “@org.junit.runners.Parameterized$Parameters(name={index}: (Quantity {0} * Price {1}) * (Discount{2}/100) = {3})” I write in method generateData is extracted by framework:

JUnit 注解@RunWith的工作原理

After that call allParameters method to retrieve test data written in test code.

(2) In allParameters method, my prepared test data is passed to JUnit framework via reflective call:

JUnit 注解@RunWith的工作原理

(3) then my test case class is instantiated by reflection, the first set of test data is passed into constructor. After that the method annotated with @Before and @Test are executed sequentially. And then, @Before and @Test will be executed once again for the second set of test data. This could be observed by id displayed in debugger.

JUnit 注解@RunWith的工作原理
JUnit 注解@RunWith的工作原理
JUnit 注解@RunWith的工作原理
JUnit 注解@RunWith的工作原理

繼續閱讀