WorkNote_Day68:认识Junit基本注解@Before、@After、@Test、@BeforeClass、@AfterClass

在学习zookeeper中偶然接触到了 Junit 中除了@Test 外的用法,特此转载记录
原文地址:Junit 实例精讲基础教程(一) 认识Junit基本注解@Before、@After、@Test、@BeforeClass、@AfterClass

  • Junit官网:http://junit.org/junit4/
  • 关于Junit,官文甚至不做过多解释:Junit只是一个用于单元测试的小框架,是基于xUnit架构的一个实现。
  • 更多精彩请阅读 东陆之滇的csdn博客:http://blog.csdn.net/zixiao217
  • 系列教程基于Maven。
  1. 引入Junit依赖

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    贴士:Junit捆绑了一个hamcrest-core的类库,查看依赖树可以看到:
    dependency:tree
    ...
    [INFO] \- junit:junit:jar:4.12:test
    [INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
    
    如果你要使用hamcrest-core的其他版本,可以过滤掉hamcrest-core:
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- This will get hamcrest-core automatically 
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>  -->
        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
        <version>1.4-atlassian-1</version>
    </dependency>
    
  1. Junit中的基本注解

    Junit中集中基本注解,是必须掌握的
    • @BeforeClass ––表示在类中的任意public static void方法执行之前执行
    • @AfterClass –– 表示在类中的任意public static void方法执行之后执行
    • @Before –– 表示在任意使用@Test注解标注的public void方法执行之前执行
    • @After –– 表示在任意使用@Test注解标注的public void方法执行之后执行
    • @Test –– 使用该注解标注的public void方法会表示为一个测试方法


      使用示例:
      BasicAnnotationTest.java:
      package org.byron4j.spring_mvc_log4j;
      
      import org.junit.After;
      import org.junit.AfterClass;
      import org.junit.Before;
      import org.junit.BeforeClass;
      import org.junit.Test;
      
      public class BasicAnnotationTest {
          // Run once, e.g. Database connection, connection pool
          @BeforeClass
          public static void runOnceBeforeClass() {
              System.out.println("@BeforeClass - runOnceBeforeClass");
          }
      
          // Run once, e.g close connection, cleanup
          @AfterClass
          public static void runOnceAfterClass() {
              System.out.println("@AfterClass - runOnceAfterClass");
          }
      
          // Should rename to @BeforeTestMethod
          // e.g. Creating an similar object and share for all @Test
          @Before
          public void runBeforeTestMethod() {
              System.out.println("@Before - runBeforeTestMethod");
          }
      
          // Should rename to @AfterTestMethod
          @After
          public void runAfterTestMethod() {
              System.out.println("@After - runAfterTestMethod");
          }
      
          @Test
          public void test_method_1() {
              System.out.println("@Test - test_method_1");
          }
      
          @Test
          public void test_method_2() {
              System.out.println("@Test - test_method_2");
          }
      }
      
      输出:
      @BeforeClass - runOnceBeforeClass
      @Before - runBeforeTestMethod
      @Test - test_method_1
      @After - runAfterTestMethod
      @Before - runBeforeTestMethod
      @Test - test_method_2
      @After - runAfterTestMethod
      @AfterClass - runOnceAfterClass
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值