2010-07-27 3 views
0

junit 4.8.1을 사용하고 있습니다.Junit @Before annotation이 Nullpointer 예외를 제공합니다.

다음은 코드입니다. "Nullponiter"예외가 발생합니다. @Before 아래의 "SetUp"코드가 다른 방법보다 먼저 실행되지 않았을 것으로 판단됩니다. 배운 친구들에게 문제 해결을 도우라고 요청하십시오.

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

} 

답변

3

당신은 같은 이름을 가진 두 개의 변수가 (이 Koskela에 의해 TDD 책에 대한 예입니다) :

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

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

변화를 마지막 라인 :

template = new Template("${one},${two},${three}"); 
+0

고마워 대한 매우 빠른 답장. 그것은 효과가 있었다. 감사합니다. Anand – Anand

+0

그래서 답변을 수락 할 수 있습니다 ;-) – djna