Shruti Seth :
I'm not sure if a question similar to this already exists but i was not able to find any helpful answers.
I have to create a list of objects that are supposed to be used by all the junit test cases.
If i write a @BeforeClass
method like this:
@BeforeClass
static public void setUp() {
MyObject object = new MyObject (int a, Stringb)
}
It does n't work because these objects are out of scope for the junit test cases
And if i write @BeforeClass
method like this:
Class Abc{
MyObject object;
@BeforeClass
static public void setUp() {
object = new MyObject (int a, Stringb)
}
}
There's a compilation error as i can't access non static variable from static class.
I could make all the objects static but that seems unnecessary to me. What is the best way to do this?
Michael :
I recommend using @Before
for your use case:
class Abc {
MyObject object;
@Before
public void setUp() {
object = new MyObject();
}
@Test
public void test() {
// Use object
}
}