java - JUnit - fields that are one time initiated in @Before are null in tests -


like title says keep getting fields null down in test cases though i've initiated them in @before method one-time set-up. exception first test runs, works.

i able replicate behaviour following code:

public class networktest extends testcase{     private static boolean onetimesetupdone = false;     private client client;      @before     public void setup(){         if(!onetimesetupdone){             client = new client();              onetimesetupdone = true;         }        }      @test     public void testconnection(){         system.out.println(client);         assertfalse(true);     }      @test     public void testmulticonnection(){         system.out.println(client);         assertfalse(true);     } 

am missing here or why fields cleared after first test run?

@before run before each test - should think of every @test running in new instance of test class, , @before sets instance (pro tip: @ class' .hashcode() - it's different in each test method).

you can use @beforeclass in case.

cheers,


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -