如何获取原始扩展的变量class

How to get a variable of the original extension class

我是 Java、Spring 和 JUnit 的初学者。我正在尝试编写使用 JUnit 5 的 @ExtendWith 的代码。我想使用在我的原始扩展 class 中使用 @before 和 @after 生成的变量(这是一个随机数)并判断测试时是偶数还是奇数。但是getX方法returns0和测试class永远获取不到变量

这是我的代码。这是一个测试class。我想获取在 SampleExtension.

中生成的变量“x”
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.*;

@ExtendWith(SampleExtension.class)
public class Exercise {
    
    //@RegisterExtension
    SampleExtension sample = new SampleExtension();
    //@RepeatedTest(5)
    @Test
    void test() {
        System.out.println("test");
        int x = sample.getX();
        System.out.println(x);
        assumingThat((x % 2 == 0), () -> {
            System.out.println("this is an even number");
        });
    }
    
}

这是一个扩展 class。

import java.util.*;
import org.junit.jupiter.api.extension.*;

public class SampleExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
    
    private int x;
    
    @Override
    public void afterEach(ExtensionContext Context) throws Exception {
        System.out.println("afterEach");
        System.out.println(this.x);
    }
    @Override
    public void beforeEach(ExtensionContext Context) throws Exception {
        
        System.out.println("beforeEach");
        this.x = (int)(Math.random() * 5) + 1;
    }
    @Override
    public void afterAll(ExtensionContext Context) throws Exception {
        System.out.println("afterAll");
        this.x = 0;
    }
    @Override
    public void beforeAll(ExtensionContext Context) throws Exception {
        System.out.println("beforeAll");
        this.x = 0;
    }
    
    int getX(){
        return this.x;
    }
}

结果。

Exercise > test() STANDARD_OUT
    beforeEach
    test
    0
    this is an even number
    afterEach
    4

实际上,@RegisterExtension(我在测试 class 中注释掉的地方)按我希望的方式工作,而不是 ExtendWith(someExtension.class),并且测试 class 可以得到变量。但我想知道如何使用 @ExtendWith.

来做到这一点

鉴于 Jupiter 的扩展生命周期的工作方式,即时变量不适合将任何类型的状态从扩展回调的一次执行传输到下一次执行。引用 Jupiter's user guide:

Usually, an extension is instantiated only once. So the question becomes relevant: How do you keep the state from one invocation of an extension to the next? The ExtensionContext API provides a Store exactly for this purpose.

所以你要做的是类似于:


    @Override
    public void beforeEach(ExtensionContext context) throws Exception    {
        System.out.println("beforeEach");
        int value = (int)(Math.random() * 5) + 1;
        getStore(context).put("MYVALUE", value);
    }

    @Override
    public void afterEach(ExtensionContext Context) throws Exception {
        System.out.println("afterEach");
        int value = (int) getStore(context).get("MYVALUE");
        System.out.println(value);
    }

    private Store getStore(ExtensionContext context) {
        return context.getStore(Namespace.create(getClass()));
    }

请注意,我还没有实际编译代码,因此其中可能存在错误。