简体   繁体   English

如何保存声明为类变量并在JUnit中的测试内初始化的任何对象

[英]How to save any object which is declared as a class variable and initialized inside a test in JUnit

I am designing test cases in JUnit . 我正在JUnit设计测试用例。 My question is how to save any object which is declared as a class variable and initialized inside a test. 我的问题是如何保存任何被声明为类变量并在测试中初始化的对象。 Currently in such cases I am getting java.lang.NullPointerException . 目前在这种情况下我得到java.lang.NullPointerException

Below is the brief description- I have three services listed below which needs to be tested- 以下是简要说明 - 我在下面列出了三项需要测试的服务 -

  1. Service 1(login): It accept a username password pair and returns cookies in response. 服务1(登录):它接受用户名密码对并返回cookie作为响应。
  2. Service 2(postmessage): It accept a message, which must be supplied with cookies in the request and returns an unique id of posted message 服务2(postmessage):它接受一条消息,该消息必须在请求中提供cookie并返回已发布消息的唯一ID
  3. Service 3(markasread) : It accept a message id, which must be supplied with cookies. 服务3(markasread) :它接受一个消息ID,必须提供cookie。 The message id, which should be used in Service 3 is returned by service 2. 服务3中应返回应在服务3中使用的消息ID。

This is what I was expecting to work- 这是我期待的工作 -

import static org.junit.Assert.*;
import org.junit.Test;

public class ProfileTest{
    private String cookie;
    private String messageId;

    @Test
    public void loginTest(){
        String username = "demouser";
        String password = "demopassword";

        HttpResponse response = login(username, password);
        cookie = response.getCookie();

        assertEquals(response.getResponse(), "success");
    }

    @Test
    public void postmessageTest(){
        String message = "Hi, this is test message";
        HttpResponse response = postmessage(message, cookie);
        messageId = response.getCookie();

        assertEquals(response.getResponse(), "success");
    }

    @Test
    public void markasreadTest(){
        HttpResponse response = markasread(messageId, cookie);

        assertEquals(response.getResponse(), "success");
    }
}

The answer is you cannot and you should not it. 答案是你不能,你不应该。 Each run of test cases will have its own copy of class variables. 每次运行的测试用例都有自己的类变量副本。 The idea behind this is just that each test case should run independently and should not depend on each other. 这背后的想法只是每个测试用例应该独立运行,不应该相互依赖。

It may be possible through some junk code but I really don't have such a recommendation for you. 有可能通过一些垃圾代码,但我真的没有这样的建议。

If possible then try to club the tescases together and run as a single one. 如果可能的话,然后尝试将测试组合在一起并作为单个测试运行。

The reason you can't do it is the BlockJUnit4ClassRunner creates a new instance of the test class per leaf method. 你不能这样做的原因是BlockJUnit4ClassRunner为每个叶子方法创建一个新的测试类实例。 It constructs the test class with the default constructor. 它使用默认构造函数构造测试类。

BlockJUnit4ClassRunner BlockJUnit4ClassRunner

The relevant code is 相关代码是

protected Object createTest() throws Exception {
    return getTestClass().getOnlyConstructor().newInstance();
}

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

相关问题 如何在 Junit 测试 class 内实例化我的接口 object? - How to instantiate my Interface object inside Junit test class? 如何为在枚举中声明的 guice 注入 static 变量编写 junit - How to write junit for guice injected static variable which is declared inside Enum 如何使用Junit测试实现Runnable的类 - How to test class which implements Runnable with Junit 如何模拟在 class java Z587FEFE304B8E3505DE89580432C?DZ25C 中初始化的新 object - How to mock for new object initialized in a class java junit? @BeforeEach 不影响测试,并且对象未初始化 - 带有 JUNIT 的 Eclipse - @BeforeEach not affect the Test, and the object not initialized - Eclipse with JUNIT Junit测试具有抽象类对象的类 - Junit Test for a class which has abstract class object 朱尼特 如何测试返回类对象的方法 - Junit . How to test a method that return a class object JUnit如何仅在测试类内部定义一次的情况下管理运行@BeforeClass - How JUnit manages running @BeforeClass only once which is defined inside the test class 如何在Java中的被测类中注入被声明为private static的对象的模拟? - How to inject the mock of an object which is declared as private static in the class under test in java? 使用jUnit测试尚未创建的类/对象 - Using jUnit to test a class / object which is not created yet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM