lundi 16 juillet 2018

junit static object in test class is not static

so this feels like a question that the internet must have answered already but I can't find it.

Given the following example, I'm finding that for each test checkIt() and checkItAgain() a new instance of ATest appears to be created(which is fine) but then there's also a new copy of my MyObject which is static and final so I'm not expecting this to change.

public class ATest {
  public static final MyObject obj = new MyObject();

  public ATest() {
    System.out.println("shouldn't be changing: " + obj.hashCode());
  }

  @Test
  public void checkIt() {
    // test stuff
  }

  @Test
  public void checkItAgain() {
    // test stuff
  }
}

So my questions are...

  1. Why am I seeing 2 instances of a static/final object?
  2. How do I share an object between tests within a class?

Thank yous

Aucun commentaire:

Enregistrer un commentaire