JUNIT Null-Zeiger-Ausnahme

es ist mein erstes mal mit JUNIT und ich buchstäblich kann es nicht funktionieren.

Ich habe eine Klasse person, in die firstName-und lastName-und eine test-Klasse, in die ich brauche zum testen der Methoden. Jedes mal wenn ich Versuch es zu testen, obwohl, wenn ich schrieb einen test für die Methode, es klappt nicht.

Hier ist mein code.

Klasse "Person"

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, die Test-Klasse

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

Kann jemand mich in die richtige Richtung?

  • Bewegen Sie die Erstellung von Person p1... zu Ihrem @Before Methode.
InformationsquelleAutor AnjunaTom | 2013-11-14
Schreibe einen Kommentar