JUNIT源码阅读(二)– org.junit.*

一、After
从4.0开始定义的一个注解@After,在org.junit.Test方法执行后才调用本方法 ,被注释的方法必须为public,同时,即使@Test方法和@Before抛错了,也会执行。
当前类的After会在父类的After之前执行(也适用于AfterClass)
@Retention(RetentionPolicy.RUNTIME)
1)、RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
2)、RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
3)、RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
使用了@Target(ElementType.METHOD),注解的作用目标为方法
1)、@Tar get(ElementType.TYPE) //接口、类、枚举、注解
  @Target(ElementType.FIELD) //字段、枚举的常量
  @Target(ElementType.METHOD) //方法
  @Target(ElementType.PARAMETER) //方法参数
  @Target(ElementType.CONSTRUCTOR) //构造函数
  @Target(ElementType.LOCAL_VARIABLE)//局部变量
  @Target(ElementType.ANNOTATION_TYPE)//注解
  @Target(ElementType.PACKAGE) ///包
二、AfterClass
如果使用了org.junit.BeforeClass,就需要执行AfterClass注解的方法,这个被注解的方法会在@Test方法后执行
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
三、Assume
从JUNIT4.4开始新增的类。Assume直译为假设,是JUnit提供的一套用于判断测试用例的入参是否有业务含义的工具,如果入参不符合预期时会抛出AssumptionViolatedException,默认的BlockJUnit4ClassRunner及其子类会捕获这个异常并跳过当前测试,如果使用自定义的Runner则无法保证行为,视Runner的实现而定。

  • public static void assumeTrue(boolean):判断输入的值是否为true,结果为false时,用例停止或者忽略,类似一个假定条件,只有达到条件后才会执行测试用例。
  • public static void assumeFalse(boolean b):判断输入的值是否为false
  • public static void assumeThat(T , Matcher ):判断两个参数是否matches,matches()可以重写,这个方法抛出AssumptionViolatedException
  • public static void assumeNotNull(Object… objects):Object …objects这种参数定义是在不确定方法参数的情况下的一种多态表现形式。即这个方法可以传递多个参数,这个参数的个数是不确定的。使用LIST中的asList()方法,调用Matcher类中的everyItem()去判断是否有对象为空
  • public static void assumeNoException(Throwable):当传入异常时,用例会被终止或者忽略
    四、Before
    从4.0开始定义的一个注解@Before,在org.junit.Test方法执行前才调用本方法 ,被注释的方法必须为public。一般写整个测试用例组执行的前置条件,例如创建被测试对象等。使用范围为:
    RetentionPolicy.RUNTIME和ElementType.METHOD
    当前类的Before会在父类的Before之后执行(也适用于BeforeClass )
    五、BeforeClass
    和before的区别
    @before
    在每个测试方法之前都会运行一次,只需声明成public
    @beforeclass
    在类中只运行一次,必须声明成public static
    六、ClassRule
    @ClassRule和@Rule只能注解在字段中,并且该字段的类型必须实现了TestRule接口,对@ClassRule注解的字段还必须是public,static,并且@ClassRule注解的字段在运行时不可以抛异常,不然JUnit的行为是未定义的
    而对@Rule注解的字段必须是public,非static
    这个没用过,具体也不是很清楚,参考了一下别人的博客,以后用到的时候进行补充。把这个包看完下一个就读TestRule和它的孩子们,算是Junit的核心部分了。。
    七、ComparisonFailure类
    这个类是在assertEquals()判断后,为了详细的描述两个判断对象具体的不一致,对比长度限制为20,具体放到AssertionError章节细看。
    八、ComparisonCompactor类
    是ComparisonFailure的私有静态类。具体放到AssertionError章节细看。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.IllegalArgumentException: Parameter 'directory' is not a directory at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:293) at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:378) at com.bosssoft.hr.train.j2se.util.UtilsDemo.method4(UtilsDemo.java:133) at Test1.testUtilsDemo4(Test1.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)源码如下:public void method4(){ // 获取目录中的所有文件和子目录 Collection<File> files = FileUtils.listFiles(new File("com/bosssoft/hr/train/j2se/util"), new String[]{"*.java"},true); // 遍历文件和子目录 if (files != null) { for (File file : files) { if (file.isDirectory()) { log.info("Directory: " + file.getName()); } else { log.info("File: " + file.getName()); } } } }
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值