Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit test class variable scope

Tags:

junit

I have a Junit test class with two tests in it. test 1 will add an element to an ArrayList and passes it to the main class to do some logic and returns true. Test two should use the same arrayList and calls the same main class do the same logic but by the time the list comes to second test methods, its getting null.

I declared it as class variable. My questions is.. when I assign values to a class variable in a test method, will I not have access to the values in next test method? If yes, how should i retain the values.

Thanks, Mahi

like image 842
Mahi Avatar asked Aug 09 '11 18:08

Mahi


1 Answers

The way variables work in jUnit is that they get initialized before each test. So if you added value x to a list in Test1, it won't be there when you run Test2. If you want to some kind of initialization before each test, use the setup method.

like image 134
K'' Avatar answered Sep 28 '22 01:09

K''