使用JUnitParams简化Parameterized tests
生活随笔
收集整理的這篇文章主要介紹了
使用JUnitParams简化Parameterized tests
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
序
junit4的Parameterized tests的使用方法太過費勁了,這里介紹下如何使用JUnitParams來簡化Parameterized tests。
junit4原生的Parameterized tests實例
@RunWith(Parameterized.class) public class FibonacciTest {@Parameterspublic static Collection<Object[]> data() {return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });}private int fInput;private int fExpected;public FibonacciTest(int input, int expected) {fInput= input;fExpected= expected;}@Testpublic void test() {assertEquals(fExpected, Fibonacci.compute(fInput));} }JUnitParams的使用
maven
<dependency><groupId>pl.pragmatists</groupId><artifactId>JUnitParams</artifactId><version>1.1.0</version><scope>test</scope> </dependency>實例
@RunWith(JUnitParamsRunner.class) public class PersonTest {@Test@Parameters({"17, false", "22, true" })public void personIsAdult(int age, boolean valid) throws Exception {assertThat(new Person(age).isAdult(), is(valid));}}junit5的更新
當然junit5也對Parameterized tests的使用進行簡化,如下:
@ParameterizedTest @EnumSource(value = TimeUnit.class, names = { "DAYS", "HOURS" }) void testWithEnumSourceInclude(TimeUnit timeUnit) {assertTrue(EnumSet.of(TimeUnit.DAYS, TimeUnit.HOURS).contains(timeUnit)); }小結
如果還是使用junit5之前的版本,那么可以嘗試使用JUnitParams來簡化Parameterized tests。如果你已經使用junit5,那么恭喜你,可以不用額外引入JUnitParams就可以方便地進行Parameterized tests。
doc
- parameterized-tests
- JUnitParams
- writing-tests-parameterized-tests
轉載于:https://my.oschina.net/go4it/blog/1488501
總結
以上是生活随笔為你收集整理的使用JUnitParams简化Parameterized tests的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于Unity中的本地存储
- 下一篇: OSChina 周四乱弹 ——程序员怎么