JUNIT空指针异常

编程入门 行业动态 更新时间:2024-10-26 04:29:24
本文介绍了JUNIT空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我第一次使用JUNIT,而我却无法使用它.

it's my first time using JUNIT and I literally cannot get it to work.

我有一个person类,其中有firstName和lastName,还有一个测试类,在其中需要测试方法.但是,每次我尝试测试一个方法时,如果我已经为特定方法编写了一个测试,它将失败.

I've got a person class, in which has firstName and lastName, and a test class in which I need to test the methods. Everytime I attempt to test one though, if i've wrote a test for the particular method, it fails.

这是我的代码.

人员班

public class Person { private String firstName; private String lastName; public Person (String a, String b) { firstName = a; lastName = b; } public String getfirstName() { return firstName; } public void setfirstName(String firstName) { this.firstName = firstName; } public String getlastName() { return lastName; } public void setlastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return firstName + " " + lastName; } }

人员测验班

public class PersonTest { Person p1; public PersonTest() { Person p1 = new Person ("Thomas", "Brown"); } @Before public void setUp() {} @After public void tearDown() {} @Test public void testGetfirstName() { assertEquals("Thomas", p1.getfirstName()); } @Test public void testSetfirstName() {} @Test public void testGetlastName() { assertEquals("Brown", p1.getlastName()); } @Test public void testSetlastName() {} @Test public void testToString() { assertEquals("Thomas Brown", p1.toString()); } }

有人能指出我正确的方向吗?

Could anyone point me in the right direction?

推荐答案

这是正确的方法:

@Before public void setUp() { p1 = new Person ("Thomas", "Brown"); }

您的代码中有2个问题.

You have 2 problems in your code.

public PersonTest() { Person p1 = new Person ("Thomas", "Brown"); }

这将创建一个局部变量,并且您的字段 p1 保持为 null .

This creates a local variable and your field p1 stays null.

第二个是在您的 setUp 方法中,您没有初始化 p1 .

The second is that in your setUp method you do not initialize p1.

用 @Before 注释的方法将在每次测试都应进行初始化之前运行.我还建议使用更具描述性的名称,因此您应该将 p1 更改为 target 或类似的名称.

The method you annotate with @Before will run before every test to you should put the initialization there. I also suggest to use more descriptive names so you should change p1 to target or something like that.

对于您的 set ... 方法,您可以执行以下操作:

For your set... methods you can do something like this:

public class PersonTest { private static final String TEST_FIRST_NAME = "some name"; Person target; // ... @Test public void testSetFirstName() { target.setFirstName(TEST_FIRST_NAME); Assert.assertEquals(target.getFirstName(), TEST_FIRST_NAME); } }

此时,您可以假定 getFirstName 可以正常工作,因为您也对此进行了测试.

At that point you can assume that getFirstName works since you have a test for it as well.

旁注:我认为,只要使用Eclipse生成getter和setter,就不必测试它们.只是不必要的.

A sidenote: I think you don't have to test getters and setters as long as you generate them with Eclipse. It is just unnesessary.

更多推荐

JUNIT空指针异常

本文发布于:2023-08-02 17:52:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   异常   JUNIT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!