dimanche 21 juillet 2019

Quick JUnit Class Test tests not failed

i am new to JUnit Testing and i need to Test a class of mine for a Student-Excercise. I want to test if the getters a working with a correct Object of Student. I also want to test if there are Exceptions thrown, when i give the constructor e.g. a empty String for name "" and also when i give a negative "Matrikelnummer".

The Tests that i wrote should detect that there is no exception thrown for a negative Matrikelnummer, but somehow the test is always passed.

Also the test for an empty string is always passed, even if its clear that the exception message is printed out in the console.

How can i do this? I am working with JUnit 4.7. I am thankful for any help.

package Model;

public class Student extends Person
{
    private int Matrikelnummer;
    private Studiengruppe studiengruppe;

    public Student(String name, String vorname, int matrikelnr, Studiengruppe studiengruppe)
    {
        super(name, vorname);
        this.Matrikelnummer = matrikelnr;
        this.studiengruppe = studiengruppe;
    }

    public Student(String name, String vorname, int matrikelnr, Studiengruppe studiengruppe,
                   String strasse, String hausnummer, String ort, int plz)
    {
        super(name, vorname, strasse, hausnummer, ort, plz);
        this.Matrikelnummer = matrikelnr;
        this.studiengruppe = studiengruppe;
    }

    public int getMatrikelnummer() {
        return Matrikelnummer;
    }
    public Studiengruppe getStudiengruppe()
    {
        return studiengruppe;
    }
}

This Test e.g. is passed even if the string is not "":

    @Test(expected = NullPointerException.class) //Test der Nullpointerexception
    public void test2() {
        try {
            System.out.println("Test 2");
            Student stud = new Student("", "Luca", 6541817, Studiengruppe.IB3A);
            System.out.println("Fail, Exception not thrown");
        }
        catch(NullPointerException ex)
        {
            System.out.println(ex.getMessage());
        }
    }

Aucun commentaire:

Enregistrer un commentaire