简体   繁体   中英

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

I am not sure you are using the right annotation there because if you catch the exception inside the test method, it should always fail as the exception won't get out of the test where it's expected. Also, there is a nice method Assert.fail("some message") which you could use after the constructor call instead of the sysout.

On the other hand, you should be aware, that when you pass in an en empty string literal (""), then it's not a null, so it shouldn't end with a nullpointer exception (except there are some logic which is not shared with us).

Also, inside the constructor, you should check the parameter values and validate them. If any values are making this object invalid, then you should throw another type of RuntimeException, because throwing nullpointer exceptions is a bad practice as the runtime will 'do it for you'. Catching them are also a bad practice because like any other runtime exception, you can validate them before the event causing a null pointer actually happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM