Junit @Before 注释给出 Nullpointer 异常
我使用的是junit 4.8.1。
以下是代码。我收到“Nullponiter”异常。我怀疑@Before
下的“SetUp”代码没有在其他方法之前执行。请求有学识的朋友帮我解决这个问题。 (这是 Koskela 的 TDD 书籍的示例)
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());
}
}
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());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有两个同名的变量:
将最后一行更改为:
You have two variables with the same name:
change that last line to: