简体   繁体   English

在 JUnit 或 TestNG 的两个测试类之间共享 Object

[英]Sharing an Object between two Test classes in either JUnit or TestNG

Forgive the elementary question, I am learning Java still so need some advice on best practice here.原谅这个基本问题,我正在学习 Java 仍然需要一些关于最佳实践的建议。 I have a valid scenario where I wish to share the same object between two distinct Test classes using JUnit or TestNG.我有一个有效的场景,我希望使用 JUnit 或 TestNG 在两个不同的测试类之间共享相同的 object。 I understand that tests/test classes should not usually share state but this is a long-running journey.我知道测试/测试类通常不应该共享 state 但这是一个漫长的旅程。

I understand the JVM executes for both frameworks in this order:我了解 JVM 按以下顺序为两个框架执行:

  1. @BeforeClass @课前
  2. Construcor call构造调用
  3. @Before @前
  4. @Test @测试

Given I have an Person class with one field name and one getter & setter for same and I instantiate an instance of it in one Test Class:假设我有一个 Person class,它有一个字段name和一个 getter & setter,我在一个测试 Class 中实例化它的一个实例:

public class FirstPersonTest {

    private Person firstPerson;

    @BeforeClass
    private void setup() {
        firstPerson = new Person("Dave");
    }

    @Test
    public void testName() {
        assertEquals("Dave", firstPerson.getName());
    }
}

And a second Test class:第二个测试 class:

public class SecondPersonTest {

    private Person firstPerson;
    private static String name;

    @BeforeClass
    private void setup(){
        name = firstPerson.getName(); //null pointer, firstPerson reference no longer exists from FirstPersonTest
    }

    @Test
    public void testName(){
        assertEquals("Dave", name);
    }
}

What is the optimal way of accessing the firstPerson object in the second class?在第二个 class 中访问firstPerson object 的最佳方式是什么? I don't want to instantiate it a second time because I wish to share state for a journey test.我不想第二次实例化它,因为我想分享 state 进行旅程测试。

I want to be able to pass firstPerson instance in the constructor or an annotated setup method, but don't wish to instantiate the SecondPersonTest within the body of FirstPersonTest我希望能够在构造函数或带注释的设置方法中传递firstPerson实例,但不希望在FirstPersonTest的主体中实例化SecondPersonTest

You can use a singleton class for this purpose.为此,您可以使用 singleton class。

public class LocalStorage {
    private static volatile LocalStorage instance;
    private Map<String, Object> data = new HashMap<>();

    private LocalStorage() {
    }

    public static LocalStorage getInstance() {
        if (instance == null) {

            synchronized (LocalStorage.class) {
                if (instance == null) {
                    instance = new LocalStorage();
                }
            }
        }

        return instance;
    }

    public static void addData(String key, Object value) {
        getInstance().data.put(key, value);
    }

    public static Object getData(String key) {
        return getInstance().data.get(key);
    }

    public static <T> T getData(String key, Class<T> clazz) {
        return clazz.cast(getInstance().data.get(key));
    }
}

You can store the whole Person object or only the name field of the Person object.您可以存储整个 Person object 或仅存储 Person object 的名称字段。

To store:储藏:

Person firstPerson = new Person("Dave");
LocalStorage.addData("Dave", firstPerson);

To get it back:要取回它:

Person firstPerson = LocalStorage.getData("Dave", Person.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM