Java JUNIT 空指针异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19978692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:02:40  来源:igfitidea点击:

JUNIT Null Pointer Exception

javatestingjunit

提问by AnjunaTom

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

这是我第一次使用 JUNIT,我真的无法让它工作。

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.

我有一个 person 类,其中有 firstName 和 lastName,还有一个 test 类,我需要在其中测试方法。每次我尝试测试一个方法时,如果我为特定方法编写了一个测试,它就会失败。

Here is my code.

这是我的代码。

Person Class

人物类

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

Person Test class

人物测试类

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?

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

采纳答案by Adam Arold

This is the right way to do it:

这是正确的方法:

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

You have 2 problems in your code.

您的代码中有 2 个问题。

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

This creates a local variable and your field p1stays null.

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

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

第二个是在你的setUp方法中你没有 initialize p1

The method you annotate with @Beforewill run before every test to you should put the initialization there. I also suggest to use more descriptive names so you should change p1to targetor something like that.

您注释的方法@Before将在每次测试之前运行,您应该将初始化放在那里。我还建议使用更具描述性的名称,因此您应该更改p1target或类似的名称。

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

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

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

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

那时你可以假设它getFirstName有效,因为你也有一个测试。

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.

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

回答by Caffe Latte

Change

改变

Person p1 = new Person ("Thomas", "Brown"); //this is local variable

to

 p1 = new Person ("Thomas", "Brown");// and this will use the instance variable