1. 程式人生 > >Junit 例項精講基礎教程(一) 認識Junit基本註解@Before、@After、@Test、@BeforeClass、@AfterClass

Junit 例項精講基礎教程(一) 認識Junit基本註解@Before、@After、@Test、@BeforeClass、@AfterClass

關於Junit,官文甚至不做過多解釋:Junit只是一個用於單元測試的小框架,是基於xUnit架構的一個實現。

系列教程基於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> </dependencies>

2. 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