JUnit Tests Konstruktor Testen

Jungs ich bin neu zu JUnit testen und zu versuchen, ein guter Griff, jetzt bin ich das schreiben von JUnit-tests für einen Konstruktor (für die Digraph-Klasse, entsteht ein gerichteter graph) throws IllegalArgumentException, wenn es liest negativen int-Wert und erstellt ein Diagramm, wenn alles in Ordnung ist (Anzahl der Knoten-Wert) ist größer als null.

Digraph-Klasse:

 In in = new In();
 public Digraph(In in) {
  try {
    this.nodes = in.readInt();
    System.out.println("Total nodes in graph: "+ nodes);
    if (nodes < 0) throw new IllegalArgumentException("Number of vertices must be > 0);
    int E = in.readInt();
    if (E < 0) throw new IllegalArgumentException("Number of edges must be >0);
  }catch (NoSuchElementException e) {
     throw new InputMismatchException("Invalid input format in Digraph constructor");
  }

Unten ist der test, den ich bin versucht zu schreiben:

@Rule
  public ExpectedException exception = ExpectedException.none();  

@Test(expected = IllegalArgumentException.class)
public void DigraphIn() {

    Digraph G = new Digraph(in.readInt());

    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("Vertices can't be nagative");
    exception.expectMessage("Invalid input format in Digraph constructor");
    exception.expectMessage("Number of edges in a Digraph must be nonnegative");
try{
}catch (AssertionError e){
    }
}

Wie soll ich testen beiden Fällen mit einem (oder zwei) Testfälle? Wenn es gibt keine-ve Wert erkannt "im" Kriege ich java.lang.AssertionError sonst test bestanden. Vielen Dank im Voraus

InformationsquelleAutor user1569891 | 2013-09-23
Schreibe einen Kommentar