I often write tests when I need to set data object for integration tests involving a few pages with forms. Herein each form on a page could be represented by a different data object class. There are are times when test end up in filling same data for a specific page while varying it for other pages and validating the overall flow. I thought instead of setting data object each time from test method I can take advantage of instance initialization block to set data for tests.Herein I have listed a smaller version of tests I usually write -This my data class which holds data for form values - textbox, drop down etc. I have not kept setters for sake of brevity -public class DataClass {private int i;private int j;public DataClass() {    i=1;    j=1;}   public int getI(){    return i;}public int getJ(){    return j;}}Following is the helper class which is used by test class to fill form data on page -public class WorkerClass {private DataClass data;public void setData(DataClass data){    this.data = data;       }public int add() {    return data.getI()+data.getJ();}public int substract() {    return data.getI()-data.getJ();}}And here is the test method.Notice that instead of calling setData of WorkerClass in each test method I used instance initialization block -public class TestClass {private DataClass data = new DataClass();WorkerClass workerClass = new WorkerClass();{    workerClass.setData(data);}@Testpublic void testAddition() {    assert workerClass.add()==0:addition Failed;}@Testpublic void testSubstraction() {    assert workerClass.substract()==0:substraction failed;        }   }My question is, am I misusing instance initialization block and should rather be calling it from each test method, even if all the test method require same set of data?
Am I misusing instance initialization?
java;design
The problem I believe with using initialization for setting test data is that you will encounter problems when an individual test changes the state of the object you are testing. All other tests executing after that specific test would be using the wrong data and may produce a false success/failure result if the test assume a specific inital state or data.If there are common tasks before each test is run and you don't want to keep writing them down for each test (which is appropriate), they probably should be put in a setUp function.@Before public void setUp() {    workerClass = new WorkerClass();    workerClass.setData(data);}Your setUp function is automatically called before each test so you do not need to explicitly call it.See http://junit.sourceforge.net/doc/cookbook/cookbook.htm