OOPM1617 Übungswoche 06 (05.12-11.12.16)

Zielsetzung für die Übung

  • Besprechung der Hausaufgabe aus letzter Woche
    • UML Klassendiagramm
  • Testen
    • Glassbox
    • Blackbox

Präsenzaufgabe zu Glassbox

    public void bar(String a, String b){
        if (a.compareTo(b) < 0){
            a = a + b;
        } else {
            if(a.length() > b.length()){
                a = a.substring(0, a.length()-1);
            } else {
                b = b.substring(0, b.length()-1);
            }
        }
    }

Für die Zweigüberdeckung sind zum Beispiel folgende Aufrufe ausreichend:
bar("informatik". "oopm"); // Zeile 3
bar("oopm","inf"); // Zeile 6
bar("oopm","informatik"); // Zeile 8

Präsenzaufgabe zu Blackbox

Gegeben ist die Spezifikation von "public char charAt(int index)" aus der Klasse String.
Erstellen Sie einen Test mit Normal-, Grenz- und Fehlerfällen.

Mögliche Lösung:

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class TestCharat {

    String a;
    String b;
    String c;
    char d;
    char e;
    char f;
    char g;

    @Before
    public void init(){
        a = "Hallo Welt";
        b = "A";
        c = "";
        d = 'o';
        e = 'H';
        f = 't';
        g = 'A';
    }

    @Test
    public void testNormalfall() {
        assertEquals(d ,a.charAt(4));
    }

    @Test
    public void testGrenzfall() {
        assertEquals(e ,a.charAt(0));
        assertEquals(f ,a.charAt(a.length()-1));
        assertEquals(g ,b.charAt(0));
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void testFehlerfall1() {
        c.charAt(0);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void testFehlerfall2() {
        a.charAt(-3);
    }

    @Test(expected = IndexOutOfBoundsException.class)
    public void testFehlerfall3() {
        a.charAt(55);
    }
}

Zielsetzung für das Programmierpraktikum

  • Testen
    • Verwendung der JUnit-API