tags:

views:

26

answers:

1

I am using junit 4.8.1.

The following is the code. I am getting "Nullponiter" exception. I suspect that the "SetUp" code under @Before is not been excecuted before other methods. Request the learned friends to help me in resolving the issue. (This is an example for TDD book by Koskela)

import org.junit.*;
import java.util.*;
import static org.junit.Assert.*;

public class TestTemplate  {
 private Template template; 
@Before
public void setUp() throws Exception{
 Template template = new Template("${one},${two},${three}");
 template.set("one","1");
 template.set("two","2");
 template.set("three","3");
}
@Test
public void testmultipleVariables() throws Exception{
 testassertTemplateEvaluatesTo("1, 2, 3");
}
@Test
public void testUnknownVariablesAreIgnored() throws Exception{

 template.set("doesnotexist","whatever");
 testassertTemplateEvaluatesTo("1, 2, 3");
}
private void testassertTemplateEvaluatesTo(String expected){


 assertEquals(expected,template.evaluate());
}

}
+2  A: 

You have two variables with the same name:

private Template template; 
@Before
public void setUp() throws Exception{

// declaring second variable here
Template template = new Template("${one},${two},${three}");

change that last line to:

template = new Template("${one},${two},${three}");
djna
Thanks a lot for the very quick reply. It worked. RegardsAnand
Anand
so you could accept the answer ;-)
djna