JUnit 注解@RunWith的工作原理

简介: 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:image.pngTest class for this calculator:

image.pngThe 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);
  }
}

image.pngThe 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:image.pngIn 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:image.pngAfter 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:image.png(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.image.pngimage.pngimage.pngimage.png


相关文章
|
6月前
导入了Junit依赖,但@Test注解依然爆红~
导入了Junit依赖,但@Test注解依然爆红~
|
6月前
|
Java 数据库 Spring
Spring Boot 学习研究笔记(四) -Junit 5 中注解特性
Spring Boot 学习研究笔记(四) -Junit 5 中注解特性
|
7月前
|
Java 测试技术 Maven
JUnit5学习之五:标签(Tag)和自定义注解
学习和实践JUnit5的标签过滤和自定义注解功能
JUnit5学习之五:标签(Tag)和自定义注解
|
Java 测试技术
单元测试_JUnit常用单元测试注解介绍及代码演示 2
单元测试_JUnit常用单元测试注解介绍及代码演示
99 0
|
Java 测试技术 数据库连接
单元测试_JUnit常用单元测试注解介绍及代码演示 1
单元测试_JUnit常用单元测试注解介绍及代码演示
115 0
|
Java Spring 容器
Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述(二)
Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述
139 0
Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述(二)
|
Java Spring 容器
Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述(一)
Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述
113 0
Spring入门+IoC注解详情【依赖注入、整合Junit、配置类】---多以案例讲述(一)
|
测试技术
Junit单元测试类、反射、注解
Junit单元测试类、反射、注解
132 0
|
测试技术 数据库连接 Android开发
Junit - 基础注解(@BeforeClass、@Before、@Test、@After、@AfterClass)
Junit - 基础注解(@BeforeClass、@Before、@Test、@After、@AfterClass)
281 0
Junit - 基础注解(@BeforeClass、@Before、@Test、@After、@AfterClass)
|
Java 测试技术 Maven
Junit5 架构、新特性及基本使用(常用注解与套件执行)
Junit5 架构、新特性及基本使用(常用注解与套件执行)