知识大全 探索JUnit 4.4 新特性[8]
Posted 函数
篇首语:一艺之成,当尽毕生之力。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 探索JUnit 4.4 新特性[8]相关的知识,希望对你有一定的参考价值。
探索JUnit 4.4 新特性[8] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
清单 理论机制举例
import static hamcrest Matchers *; //指定接下来要使用的Matcher匹配符import static junit Assume *; //指定需要使用假设assume*来辅助理论Theoryimport static junit Assert *; //指定需要使用断言assert*来判断测试是否通过
import junit experimental theories DataPoint; //需要使用注释@DataPoint来指定数据集import junit experimental theories Theories; //接下来@RunWith要指定Theories classimport junit experimental theories Theory; //注释@Theory指定理论的测试函数import junit runner RunWith; //需要使用@RunWith指定接下来运行测试的类
import junit Test;
//注意 必须得使用@RunWith指定Theories class@RunWith(Theories class)public class TheoryTest
//利用注释@DataPoint来指定一组数据集 这些数据集中的数据用来证明或反驳接下来定义的Theory理论 //testNames 和testNames 这两个理论Theory测试函数的参数都是String 所以Junit 会将这 个 //@DataPoint定义的String进行两两组合 统统一一传入到testNames 和testNames 中 所以参数名year //和name是不起任何作用的 同样有机会会传给参数name Works 也同样有机会传给参数year @DataPoint public static String YEAR_ = ; @DataPoint public static String YEAR_ = ; @DataPoint public static String NAME = developer ; @DataPoint public static String NAME = Works ; @DataPoint public static String NAME = developerWorks ;
//注意 使用@Theory来指定测试函数 而不是@Test @Theory public void testNames ( String year String name ) assumeThat( year is( ) ); //year必须是 否则跳过该测试函数 System out println( year + + name ); assertThat( year is( ) ); //这里的断言语句没有实际意义 这里举此例只是为了不中断测试
//注意 使用@Theory来指定测试函数 而不是@Test @Theory public void testNames ( String year String name ) assumeThat(year is( )); //year必须是 否则跳过该测试函数 //name必须既不是 也不是 否则跳过该测试函数 assumeThat(name allOf( not(is( )) not(is( )))); System out println( year + + name ); assertThat( year is( ) ); //这里的断言语句没有实际意义 这里举此例只是为了不中断测试
结果输出:
第一个Theory打印出
developer Works developerWorks第二个Theory打印出
developer Works developerWorks结束语
本文通过详细深入的理论介绍和简单易懂的实例全面剖析了 JUnit 的三个新特性
提供了新的断言语法(assertion syntax)——assertThat提供了假设机制(assumptions)
提供了理论机制(Theories)
相信读者看完后一定会对 JUnit 有着非常深入的了解并可以轻松将其运用到自己的开发工程中
cha138/Article/program/Java/ky/201311/29104相关参考