全局时间戳变量在多个Java方法中使用时返回空值。

huangapple go评论75阅读模式
英文:

Global Timestamp variable returns null value when used in multiple java methods

问题

我遇到了一个奇怪的情况,一个本地时间戳变量在测试方法A中完美地工作并返回了ArrayList的正确第一个值。现在,我想在测试方法B中使用该变量,所以我将变量转换为一个在类级别声明的全局变量。奇怪的是,从ArrayList中返回的第一个值在测试方法B中是null,很明显,问题出在第二个方法没有正确读取该变量。第一个方法能够正常读取。我不确定我做错了什么。我尝试了以下方法:

private static Timestamp s; // 在全局范围声明的变量
private static final TestDao TEST_DAO = new TestDao();

// 第一个测试方法
@Test
public void testById() {
    List<TestEntity> tests = TEST_DAO.findById("");
    List<Timestamp> myDate = new ArrayList<>();
    
    for (TestEntity test: tests) {
        myDate.add(test.getDateColumn()); // 将列表元素添加到数组中
    }
          
    s = myDate.get(0); // 从数组中获取第一个元素
    System.out.println("The first element is " + s); // 变量s成功返回数组的第一个元素
}

// 第二个测试方法
@Test
public void verifyMe() {
    LocalDateTime dateTime = LocalDateTime.now().plus(Duration.of(1, ChronoUnit.MINUTES));
    Date nowPlusOneeMinutes = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
          
    System.out.println("The first element is " + s); // 这里s返回null值,尽管它被声明为类级别的私有静态变量
}
英文:

I am experiencing a weird situation where a local timestamp variable works perfectly and returns the correct first value of an ArrayList in test method A. Now, I want to use that variable in a test method B, so I turned the variable into a global variable, declared at the class level. Strangely, the first value returned from the ArrayList id test method B is null, Clearly, the problem is with the second method not correctly reading the variable. The first method reads it OK. I am not sure what I have done wrong. This is what I tried:

private static Timestamp s; //variable declared globally
private static final TestDao TEST_DAO = new TestDao();

// 1st test method
  @Test
    public void testById() {
    List&lt;TestEntity&gt; tests = TEST_DAO.findById(&quot;&quot;);
    List&lt;Timestamp&gt; myDate = new ArrayList&lt;&gt;();

    for (TestEntity test: tests) {
    myDate.add(test.getDateColumn());//A list element added to the array
    }
      
    s = myDate.get(0); //Gets the first element from the list
    System.out.println(&quot;The first element is &quot; + s); //The variable s successfully returns the first element of the array


//2nd test method
  @Test
    public void verifyMe() {
    LocalDateTime dateTime = LocalDateTime.now().plus(Duration.of(1, ChronoUnit.MINUTES));
    Date nowPlusOneeMinutes = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
      
    System.out.println(&quot;The first element is &quot; + s); //s returns a null value here even though it&#39;s declared as private static as class level

    }

答案1

得分: 2

首先,Java没有“全局变量”的概念。

private static Timestamp s; //在全局范围内声明的变量
private static final TestDao TEST_DAO = new TestDao();

// 第一个测试方法
@Test
public void testById() {
    List<TestEntity> tests = TEST_DAO.findById("");
    List<Timestamp> myDate = new ArrayList<>();

    for (TestEntity test : tests) {
        myDate.add(test.getDateColumn());//将列表元素添加到数组中
    }

    Timestamp s = myDate.get(0); //从数组中获取第一个元素
    System.out.println("第一个元素是 " + s); //变量 s 成功返回数组的第一个元素
}

是的,你的确有一个名为 s 的实例变量,但是你设置的变量是局部于你的测试方法的。它只存在于该测试的作用域内,不会改变其他测试方法可以访问的 s。

即使现在可能不同,但不能确定测试会按照你希望的顺序执行。
如果你希望在每个测试之前设置该值,请添加以下内容:

@Before
public void init() {
    List<TestEntity> tests = TEST_DAO.findById("");
    List<Timestamp> myDate = new ArrayList<>();
    
    for (TestEntity test : tests) {
        myDate.add(test.getDateColumn());//将列表元素添加到数组中
    }
      
    s = myDate.get(0); // 这将在实例级别上实际设置 s 的值
}

这样,你可以在两个测试中都使用这个元素,而且这两个测试不会相互依赖。

英文:

First of all, Java doesn't have the concept of "Global variables"

private static Timestamp s; //variable declared globally
private static final TestDao TEST_DAO = new TestDao();

// 1st test method
  @Test
    public void testById() {
    List&lt;TestEntity&gt; tests = TEST_DAO.findById(&quot;&quot;);
    List&lt;Timestamp&gt; myDate = new ArrayList&lt;&gt;();

    for (TestEntity test: tests) {
    myDate.add(test.getDateColumn());//A list element added to the array
    }
      
    Timestamp s = myDate.get(0); //Gets the first element from the list
    System.out.println(&quot;The first element is &quot; + s); //The variable s successfully returns the first element of the array
}

Yes, you do have an instant variable of the name s, but the variable you set, is local to your test method. It only exists within the scope of that test, and doesn't alter the s that can be reached by the other test.

Even if, maybe now it is different, but it's not certain the tests execute in the order you want them to.
If you want that value to be set right before each test, add this:

@Before
public void init() {
List&lt;TestEntity&gt; tests = TEST_DAO.findById(&quot;&quot;);
        List&lt;Timestamp&gt; myDate = new ArrayList&lt;&gt;();
    
        for (TestEntity test: tests) {
        myDate.add(test.getDateColumn());//A list element added to the array
        }
          
        s = myDate.get(0); // this will actually set the value of the s on instance level
}

This way, you have the element in both tests, yet the test are not dependent of each other.

huangapple
  • 本文由 发表于 2020年8月17日 20:25:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63450769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定