junit 运行_运行,JUnit! 跑!!!
junit 運行
JUnit與JavaScript和SVN一起是程序員經常開始使用的一些技術,甚至沒有讀過一篇博客文章,更不用說一本書了。 也許這是一件好事,因為它們看起來足夠簡單并且易于理解,因此我們可以立即使用它們而無需任何手冊,但這也意味著它們還沒有得到充分利用。 在本文中,我們將介紹一些我認為非常有用的JUnit功能。
參數化測試
有時我們需要使用許多不同的輸入和不同的預期結果來運行相同的方法或功能。 一種方法是為每種情況創建單獨的測試,或者您可以使用循環,但是要跟蹤可能的測試失敗的源將更加困難。
例如,如果我們有以下表示有理數的值對象:
public class RationalNumber {private final long numerator;private final long denominator;public RationalNumber(long numerator, long denominator) {this.numerator = numerator;this.denominator = denominator;}public long getNumerator() {return numerator;}public long getDenominator() {return denominator;}@Overridepublic String toString() {return String.format("%d/%d", numerator, denominator);} }我們有一個名為App的服務類,其中包含一個方法轉換,該方法將數字除以四舍五入的小數位數5:
public class App {/*** THE Logic** @param number some rational number* @return BigDecimal rounded to 5 decimal points*/public static BigDecimal convert(RationalNumber number) {BigDecimal numerator = new BigDecimal(number.getNumerator()).setScale(5, RoundingMode.HALF_UP);BigDecimal result = numerator.divide(new BigDecimal(number.getDenominator()),RoundingMode.HALF_UP);return result;} }對于實際的AppTest類,我們有
@RunWith(Parameterized.class) public class AppTest {private RationalNumber input;private BigDecimal expected;public AppTest(RationalNumber input, BigDecimal expected) {this.input = input;this.expected = expected;}@Parameterized.Parameters(name = "{index}: number[{0}]= {1}")public static Collection<Object> data() {return Arrays.asList(new Object[][]{{new RationalNumber(1, 2), new BigDecimal("0.50000")},{new RationalNumber(1, 1), new BigDecimal("1.00000")},{new RationalNumber(1, 3), new BigDecimal("0.33333")},{new RationalNumber(1, 5), new BigDecimal("0.20000")},{new RationalNumber(10000, 3), new BigDecimal("3333.33333")}});}@Testpublic void testApp() {//given the test data//whenBigDecimal out = App.convert(input);//thenAssert.assertThat(out, is(equalTo(expected)));}}參數化的運行器或@RunWith(Parameterized.class)啟用“參數化”,換句話說,將使用@ Parameterized.Parameters注釋的值的集合注入到Test構造函數中,其中每個子列表都是一個參數列表。 這意味著data()方法中的每個RationalNumber對象將被注入到輸入變量中,而每個BigDecimal值將是期望值,因此在我們的示例中,我們有5個測試。
注釋中還添加了對生成的測試的可選自定義命名,因此“ {index}:number [{0}] = {1} ”將被data()方法中定義的適當參數和“ {index}”占位符將成為測試用例索引,如下圖所示
| 在IntelliJ Idea中運行參數化測試 |
JUnit規則的最簡單定義是,它們在某種意義上是攔截器,并且與面向Spring方面的編程或Java EE攔截器API非常相似。 基本上,您可以在執行測試之前和之后做一些有用的事情。
好的,讓我們從一些內置的測試規則開始。 其中之一是ExternalResource ,其想法是我們設置一個外部資源,并在拆卸小工具后釋放該資源。 這種測試的一個經典示例是創建文件,因此,我們有一個內置的TemporaryFolder類,但我們也可以為其他資源創建自己的類:
public class TheRuleTest {@Rulepublic TemporaryFolder folder = new TemporaryFolder();@Testpublic void someTest() throws IOException {//givenfinal File tempFile = folder.newFile("thefile.txt");//whentempFile.setExecutable(true) ;//thenassertThat(tempFile.canExecute(), is(true));} }我們本可以在@Before和@After塊中完成此操作,并使用Java臨時文件,但在某些測試失敗的情況下,很容易忘記某些東西,并且未關閉某些文件。
例如,對于方法也有一個超時規則,如果在給定的時間限制內執行未完成,則測試將失敗并顯示超時異常。 例如,將運行時間限制為20毫秒:
@Rule public MethodRule globalTimeout = new Timeout(20);我們可以實施自己的規則,以執行政策實施或各種項目特定的更改。 唯一需要做的就是讓我們實現TestRule接口。
解釋該行為的一個簡單方案是添加一個在測試之前和之后都打印一些東西的規則。
import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement;public class MyTestRule implements TestRule {public class MyStatement extends Statement {private final Statement statement;public MyStatement(Statement statement) {this.statement = statement;}@Overridepublic void evaluate() throws Throwable {System.out.println("before statement" );statement.evaluate();System.out.println("after statement");}}@Overridepublic Statement apply(Statement statement,Description description) {System.out.println("apply rule");return new MyStatement(statement);}}因此,既然有了規則,我們就可以在測試中使用它,因為測試只會打印出不同的值:
public class SomeTest {@Rulepublic MyTestRule folder = new MyTestRule();@Testpublic void testA() {System.out.println("A");}@Testpublic void testB() {System.out.println("B");} }當我們運行測試時,將在控制臺輸出上創建以下輸出:
apply rule before statement A after statement apply rule before statement B after statement內置的一個名為ExpectedException的異常在嘗試測試錯誤時非常有用。 此外,還有一個選項可以鏈接規則,這些規則在許多情況下都非常有用。
總結一下
如果您想說Spock或TestNG或在JUnit之上構建的某些庫比JUnit具有更多的功能,那可能就是事實。
但是你知道嗎? 我們在類的路徑上并不總是有這些,并且JUnit可能已經存在并且已經在各處使用。 為什么不利用它的全部潛力呢?
翻譯自: https://www.javacodegeeks.com/2013/12/run-junit-run.html
junit 運行
總結
以上是生活随笔為你收集整理的junit 运行_运行,JUnit! 跑!!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑系统垃圾清理命令程序(电脑垃圾清理运
- 下一篇: 将Host Cobol批次和Monoli