0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · 课时

测试生命周期与顺序

控制测试的执行顺序,并使用高级生命周期注解管理测试准备与清理。

测试生命周期与顺序 是 CoddyKit 上的免费 Testing Mastery: JUnit, Mockito & Integration Tests 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Testing Mastery: JUnit, Mockito & Integration Tests 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

JUnit Test Lifecycle Intro

Welcome! In this lesson, we'll dive into advanced JUnit 5 features to control how your tests run. Understanding the test lifecycle is key to managing resources and ensuring proper test setup and cleanup.

The lifecycle defines the sequence of actions JUnit takes before and after your test methods and classes.

Class-Level Setup: @BeforeAll

Sometimes, you need to perform setup operations only once for all tests within a specific test class. The @BeforeAll annotation marks a method to run before any test method in that class.

By default, @BeforeAll methods must be static. We'll see how to change this soon!

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BeforeAllDemo {
    private static StringBuilder log = new StringBuilder();

    @BeforeAll
    static void setupOnce() {
        log.append("-> @BeforeAll: Setup done once.\n");
        System.out.println("Setup for all tests.");
    }

    @Test
    void testA() {
        log.append("  -> @Test: Running testA.\n");
        System.out.println("  Running testA.");
        assertTrue(true);
    }

    @Test
    void testB() {
        log.append("  -> @Test: Running testB.\n");
        System.out.println("  Running testB.");
        assertTrue(true);
    }
    // The log output would typically be verified in @AfterAll or debugger.
}

Class-Level Teardown: @AfterAll

Similarly, you might need to clean up resources once after all tests in a class have completed. The @AfterAll annotation marks a method to run after all test methods in the class.

Like @BeforeAll, it must be static by default.

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AfterAllDemo {
    private static StringBuilder log = new StringBuilder();

    @BeforeAll
    static void setup() {
        log.append("-> @BeforeAll: Setup for AfterAllDemo.\n");
    }

    @Test
    void testOne() {
        log.append("  -> @Test: Running testOne.\n");
        assertTrue(true);
    }

    @Test
    void testTwo() {
        log.append("  -> @Test: Running testTwo.\n");
        assertTrue(true);
    }

    @AfterAll
    static void teardown() {
        log.append("-> @AfterAll: Teardown for AfterAllDemo.\n");
        System.out.println("--- Execution Log ---");
        System.out.println(log.toString()); // See the full order
        System.out.println("--- End Log ---");
    }
}

@TestInstance: PER_CLASS Lifecycle

By default, JUnit creates a new instance of your test class for each test method (Lifecycle.PER_METHOD). This ensures test isolation.

If you need @BeforeAll and @AfterAll methods to be non-static, or if you want to share state across all tests in a class, you can switch the test instance lifecycle to Lifecycle.PER_CLASS.

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import static org.junit.jupiter.api.Assertions.assertEquals;

@TestInstance(Lifecycle.PER_CLASS) // Allows non-static @BeforeAll/@AfterAll
public class PerClassLifecycleDemo {
    private int counter = 0; // Instance variable, shared across tests

    @BeforeAll // No longer needs to be static!
    void setupPerClass() {
        counter = 10; 
        System.out.println("-> @BeforeAll (PER_CLASS). Counter: " + counter);
    }

    @Test
    void testIncrementOne() {
        counter++;
        System.out.println("  -> @Test 1. Counter: " + counter);
        assertEquals(11, counter); // Shared state is modified
    }

    @Test
    void testIncrementTwo() {
        counter++;
        System.out.println("  -> @Test 2. Counter: " + counter);
        assertEquals(12, counter); // Counter continues from previous test
    }

    @AfterAll // No longer needs to be static!
    void teardownPerClass() {
        System.out.println("-> @AfterAll (PER_CLASS). Final Counter: " + counter);
    }
}

Test Ordering: When to Use

Ideally, JUnit tests should be independent and run correctly in any order. Relying on a specific order can make your tests fragile and harder to maintain.

However, there are specific scenarios where explicit ordering might be useful:

  • Testing a complex workflow with clear, sequential steps.
  • Optimizing performance by grouping fast tests or tests with shared expensive setup.
  • Working with legacy systems that demand a certain interaction sequence.

@TestMethodOrder Annotation

To define an execution order for test methods within a class, you use the @TestMethodOrder annotation at the class level. It takes a MethodOrderer implementation as an argument.

JUnit 5 provides several built-in strategies:

  • Alphanumeric.class: Orders by method name alphabetically.
  • OrderAnnotation.class: Uses the @Order annotation.
  • Random.class: Randomizes the order.
  • DisplayName.class: Orders by method display names.

Alphanumeric Method Order

Let's see MethodOrderer.Alphanumeric.class in action. JUnit will simply sort test methods based on their names in alphabetical order.

This is a simple way to get a predictable order if your method names naturally follow a sequence.

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.Alphanumeric;
import static org.junit.jupiter.api.Assertions.assertTrue;

@TestMethodOrder(Alphanumeric.class) // Tests run A, B, C
public class AlphanumericOrderDemo {

    @Test
    void testA_First() {
        System.out.println("-> Running testA_First");
        assertTrue(true);
    }

    @Test
    void testC_Third() {
        System.out.println("-> Running testC_Third");
        assertTrue(true);
    }

    @Test
    void testB_Second() {
        System.out.println("-> Running testB_Second");
        assertTrue(true);
    }
}

Custom Order with @Order

For fine-grained control, combine @TestMethodOrder(MethodOrderer.OrderAnnotation.class) with the @Order(value) annotation on individual test methods. Lower value numbers mean earlier execution.

This allows you to define a precise, custom order for your tests.

import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import static org.junit.jupiter.api.Assertions.assertTrue;

@TestMethodOrder(OrderAnnotation.class) // Activate @Order annotation
public class CustomOrderDemo {

    @Test
    @Order(2) // This test runs second
    void processStepTwo() {
        System.out.println("-> Running processStepTwo");
        assertTrue(true);
    }

    @Test
    @Order(1) // This test runs first
    void initializeSystem() {
        System.out.println("-> Running initializeSystem");
        assertTrue(true);
    }

    @Test
    @Order(3) // This test runs third
    void finalizeReport() {
        System.out.println("-> Running finalizeReport");
        assertTrue(true);
    }
}

Ordering Best Practices

While powerful, explicit test ordering should be used with caution:

  • Prioritize Independence: Each test should ideally be able to run on its own.
  • Readability: Over-ordering can make tests harder to understand and maintain.
  • Maintenance Burden: Changes to the system might require re-evaluating and updating order values.

Only use ordering when there's a strong, justified reason, and prefer @Order for clear intent.

Lifecycle & Order Check

Consider the following JUnit 5 test class:

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class QuizTest {
    private int sharedValue = 0;

    @BeforeAll
    void setup() {
        sharedValue = 10;
        System.out.println("BeforeAll: sharedValue = " + sharedValue);
    }

    @Test
    @Order(2)
    void testB() {
        sharedValue += 2;
        System.out.println("  TestB: sharedValue = " + sharedValue);
        assertEquals(13, sharedValue);
    }

    @Test
    @Order(1)
    void testA() {
        sharedValue += 1;
        System.out.println("  TestA: sharedValue = " + sharedValue);
        assertEquals(11, sharedValue);
    }

    @AfterAll
    void teardown() {
        System.out.println("AfterAll: sharedValue = " + sharedValue);
    }
}

What will be the final value of sharedValue printed in the @AfterAll method?

Recap: Lifecycle & Ordering

In this lesson, you mastered advanced JUnit 5 features for controlling test execution:

  • Class-Level Lifecycle: @BeforeAll and @AfterAll for one-time setup/teardown.
  • Instance Lifecycle: @TestInstance(Lifecycle.PER_CLASS) to enable shared state and non-static class-level hooks.
  • Test Method Ordering: @TestMethodOrder (with strategies like Alphanumeric or OrderAnnotation) to define execution order.
  • Custom Order: @Order for precise control over method sequence.

Remember to use explicit ordering judiciously, prioritizing independent and robust tests!

常见问题解答

「测试生命周期与顺序」课时是免费的吗?

是的 — 「测试生命周期与顺序」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Testing Mastery: JUnit, Mockito & Integration Tests 课程的其余内容,请升级到 CoddyKit PRO。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。

「测试生命周期与顺序」这节课中我会学到什么?

控制测试的执行顺序,并使用高级生命周期注解管理测试准备与清理。 你通过在浏览器中直接运行的动手代码来练习 Testing Mastery: JUnit, Mockito & Integration Tests,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Testing Mastery: JUnit, Mockito & Integration Tests 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Testing Mastery: JUnit, Mockito & Integration Tests 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「测试生命周期与顺序」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Testing Mastery: JUnit, Mockito & Integration Tests 课中编写并运行代码吗?

能。每节 Testing Mastery: JUnit, Mockito & Integration Tests 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 测试生命周期与顺序
  2. 参数化测试与动态测试
  3. 异常测试与超时
  4. 条件测试与假设
← 返回 Testing Mastery: JUnit, Mockito & Integration Tests