简体   繁体   中英

What happens to class level variables in Junits?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM