Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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);
}

@Test
public void testAddition() {
    assert workerClass.add()==0:"addition Failed";

}

@Test
public 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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
excellent, I see clear disadvantage now. –  Tarun Dec 28 '11 at 16:32

Forget about 'should' and think about the goal: minimal tested code meeting the user's needs. The shortest (tested) code is best. If all your test methods require the same data, then there's no point in duplicating it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.