mercredi 15 mars 2017

JUnit how to test multiple functions

My question is: if I have a method to test, let's say a constructor for an object representing a person: public void Person(String name){this.name = name;}, is it silly to create one test case like:

public class PersonTest {

@Test
public void testPerson() throws myException{
    // First thing I want to test
    try {
        new Person("name to looooooooooooooong");
        fail("This test was supposed to throw an exception: name too long");
    } catch (Exception e) {
        if (e instanceof myException)
            assertEquals("MSG: name not valid!", "Name not valid", e.getMessage());
    }
    //Second thing I want to test
    try {
        new Person("name to short");
        fail("This test was supposed to throw an exception: name too short");
    } catch (Exception e) {
        if (e instanceof myException)
            assertEquals("MSG: name not valid!", "Name not valid", e.getMessage());
    }
    //Oter things I want to test ...
}

Or should I create one test suit for each object and one test case for each method to be testes? But what if I want to test several parameters for a method? should I write a test case for each case? Like:

  • one test case for a name too long
  • one test case for name too short
  • one test case for name containing numbers and so on ?

Aucun commentaire:

Enregistrer un commentaire