Home > Mobile >  What happens to class level variables in Junits?
What happens to class level variables in Junits?

Time:12-05

I have a test class that have a class level date time formatter defined as

class testClass{
DateTimeFormatter dateTimeFormatter = new DateTimeFormat.forPattern(settings.getFormatFromUser());

@Test
public void test01(){
   ...
   DateTime date = dateTimeFormatter.print(new DateTime());
   ....
}

}

And then I have a test written in same class which will use this formatter. If I change the formatter string from UI in between running the test what will happen?

Will this be a good practice to have this instantiation in test method or class level?

Just asking for my understanding of how junits run.

CodePudding user response:

Will this be a good practice to have this instantiation in test method or class level?

Without seeing the rest of your code under test and the tests themselves it's difficult for anyone to answer this question. However, it's generally a good practice to have each test method self contained so there are no unexpected side effects between each test method execution.

If you are looking for ways to do setup before each test method executes (to ensure the code under test is in the same known state before each test executes, look at using the @Before annotations to help with your setup.

  • Related