JUnit příklad na použití 1
Zadání příkladu Převod mezi římskými a arabskými číslicemi. Platí následující pravidla: hodnoty písmen se až na výjimky sčítají (I, II, VIII) Písmena vyjadřující mocniny se mohou opakovat max. 3. Písmena vyjadřující 5, 50, 500 se neopakují. Písmena římského čísla se vyjadřují od největšího po nejmenší, záleží na pořadí. 2
Zadání příkladu Pro každou hodnotu existuje pouze jeden správný zápis římskými číslicemi. Pomocířímských číslic lze zapsat čísla v rozsahu 1 3999. 3
Vytvoření kostry třídy Třída Roman bude obsahovat dvě statické metody pro převod toroman(int) a fromroman(string). bude-li vstupní parametr toroman(int) mimo rozsah, vyvolá se výjimka IllegalArgumentException. bude-li argument metody fromroman(string) obsahovat nepřípustný znak, vyvolá se výjimka NumberFormatException. 4
public class Roman { public static int fromroman(string s) throws NumberFormatException { int vysl = 0; return vysl; Základní kostra programu public static String toroman(int i) throws IllegalArgumentException { String vysl= ""; return vysl;
public class RomanTest extends TestCase{ public RomanTest(String name) { // musí specifikovat konstruktor super(name name); protected void setup() { // počáte teční nastavení protected void teardown() { // úklid po testování // testy postupně doplnované... // obyčejn ejně deklarovaná statická metoda suite // v jejímž rámci se vytvoří seznam testů public static Test suite() { return new TestSuite(RomanTest RomanTest.class class); public static void main(string String[] args) { junit.textui textui.testrunner TestRunner.run(.run(suite suite());
// Pro vytvořen ení seznamu testů se většinouv používá příkaz kaz: return new TestSuite(RomanTest.class); //do seznamu testů se zahrnou všechny v metody začínaj nající řet etězcem test test // preferovaný způsob TestSuite seznamtestu = new TestSuite(); seznamtestu.addtest(new RomanTest( testtoromanknownvalues testtoromanknownvalues ); seznamtestu.addtest(new RomanTest( testfromromanknownvalues testfromromanknownvalues );... return seznamtestu; Alternativa metodě suite postupné přidávání testů
Testy správnosti nadefinujeme dva testy: testtoromanknownvalues() a testfromromanknownvalues() 8
class Dvojice { int arab; String roman; Dvojice(int int arab, String roman) { this.arab = arab; this.roman = roman; ; Dvojice knownvalues[] = { new Dvojice(1,"I"), new Dvojice(2,"II"), new Dvojice(3,"III"), new Dvojice(4,"IV"), new Dvojice(5,"V"), new Dvojice(6,"VI"), new Dvojice(7,"VII"), new Dvojice(8,"VIII"), new Dvojice(9,"IX"), new Dvojice(10,"X"), new Dvojice(50,"L"), new Dvojice(100,"C"), new Dvojice(500,"D"), new Dvojice(1000,"M"), new Dvojice(31,"XXXI"), new Dvojice(148,"CXLVIII"), new Dvojice(3940,"MMMCMXL"), new Dvojice(3999,"MMMCMXCIX"), ; Třída Dvojice, pole hodnot se správnými dvojicemi
public void testtoromanknownvalues() { i=0; i<knownvalues knownvalues.length length; i++){ String vysl= Roman.toRoman toroman(knownvalues knownvalues[i]. [i].arab arab); assertequals(knownvalues knownvalues[i]. [i].roman roman, vysl); Testy správnosti public void testfromromanknownvalues() { i=0; i< knownvalues.length length; i++){ int vysl= Roman.fromRoman fromroman(knownvalues knownvalues[i]. [i].roman roman); assertequals(knownvalues knownvalues[i]. [i].arab arab, vysl);
Testy výjimek Testy výjimek otestují, zda se v případě chybných vstupních parametrů bude zahlášena chyba metody. Odchytávání výjimek se provádí v kódu testu, v případě, že se výjimka neobjeví, ohlásí se chyba pomocí metody JUnit fail(string textchyby) 11
public void testtoromanexception() { int badvalues[] = {0, -1, 4000; i=0; i<badvalues badvalues.length length; i++){ try{ String vysl= Roman.toRoman toroman(badvalues badvalues[i]); catch catch(illegalargumentexception e) { continue; fail("expected IllegalArgumentException"); // test vyjimky public void testillegalcharacters() { String badvalues[] = {"I ", "i", "a", "mm", "d", "MCi" MCi"; i=0; i< badvalues.length length; i++) { try { int vysl= Roman.fromRoman fromroman(badvalues badvalues[i]); catch (NumberFormatException e) { continue; fail("expected IllegalArgumentException for: "+ badvalues[i]); ;
public void testsanity() { i= 1; i< 4000; i++){ int vysl = Roman.fromRoman fromroman(roman. (Roman.toRoman toroman(i)); assertequals(i, vysl); Test vztahů závislosti
Vlastní třída a testování Do třídy Roman metody toroman() - doplnit test intervalu hodnot public static String toroman(int i) throws IllegalArgumentException { if((i>3999) (i<= 0)) throw new IllegalArgumentException(); else { return vysl; 14
Vlastní třída a testování Dále je třeba doplnit třídu Dvojice a tabulku pro převod a doplnit odpovídajícím způsobem metodu toroman() Výpis třídy Roman Výpis třídy RomanTest 15
public class Roman { public static int fromroman(string s) throws NumberFormatException { int vysl = 0; return vysl; public static String toroman(int i) throws IllegalArgumentException { if((i>3999) (i<= 0)) throw new IllegalArgumentException(); else { int cislo =i; String vysl= ""; while(cislo >0) { j=0; j< tabulka.length length; j++){ if(cislo >= tabulka[j].arab arab) { vysl= vysl+ tabulka[j].roman roman; cislo= cislo- tabulka[j].arab arab; break; return vysl; static class Dvojice { int arab; String roman; Dvojice(int int arab, String roman){ this.arab = arab; this.roman = roman; Třída Roman
static Dvojice tabulka[] = { new Dvojice (1000, "M"), new Dvojice (900, "CM"), new Dvojice (500, "D"), new Dvojice (400, "CD"), new Dvojice (100, "C"), new Dvojice (90, "XC"), new Dvojice (50, "L"), new Dvojice (40, "XL"), new Dvojice (10, "X"), new Dvojice (9, "IX"), new Dvojice (5, "V"), new Dvojice (4, "IV"), new Dvojice (1, "I"), ; Třída Roman
public class RomanTest extends TestCase{ class Dvojice { int arab; String roman; Dvojice(int int arab, String roman) { this.arab = arab; this.roman = roman; ; Dvojice knownvalues[] = { new Dvojice(1,"I"), new Dvojice(2,"II"), new Dvojice(3,"III"), new Dvojice(4,"IV"), new Dvojice(5,"V"), new Dvojice(6,"VI"), new Dvojice(7,"VII"), new Dvojice(8,"VIII"), new Dvojice(9,"IX"), new Dvojice(10,"X"), new Dvojice(50,"L"), new Dvojice(100,"C"), new Dvojice(500,"D"), new Dvojice(1000,"M"), new Dvojice(31,"XXXI"), new Dvojice(148,"CXLVIII"), new Dvojice(3940,"MMMCMXL"), new Dvojice(3999,"MMMCMXCIX"), ; Třída RomanTest
public RomanTest(String name) { super(name name); protected void setup() { protected void teardown() { // testy postupne doplnovane public void testtoromanknownvalues() { i=0; i<knownvalues knownvalues.length length; i++){ String vysl= Roman.toRoman toroman(knownvalues knownvalues[i]. [i].arab arab); assertequals(knownvalues knownvalues[i]. [i].roman roman, vysl); public void testfromromanknownvalues() { i=0; i< knownvalues.length length; i++){ int vysl= Roman.fromRoman fromroman(knownvalues knownvalues[i]. [i].roman roman); assertequals(knownvalues knownvalues[i]. [i].arab arab, vysl);
// test vyjimky public void testtoromanexception() { int badvalues[] = {0, -1, 4000; i=0; i<badvalues badvalues.length length; i++){ try{ String vysl= Roman.toRoman toroman(badvalues badvalues[i]); catch catch(illegalargumentexception e) { continue; fail("expected IllegalArgumentException"); // test vyjimky public void testillegalcharacters() { String badvalues[] = {"I ", "i", "a", "mm", "d", "MCi" MCi"; i=0; i< badvalues.length length; i++) { try { int vysl= Roman.fromRoman fromroman(badvalues badvalues[i]); catch (NumberFormatException e) { continue; fail("expected IllegalArgumentException for: "+ badvalues[i]); ;
public void testtoomanyrepeatednumerals() { String badvalues[] = {"MMMM", "VV", "LL", "CCCC", "DD", "IIII"; i=0; i< badvalues.length length; i++) { try { int vysl= Roman.fromRoman fromroman(badvalues badvalues[i]); catch (NumberFormatException e) { fail("expected IllegalArgument for: "+ badvalues[i]); ; public void testrepeatedpairs() { String badvalues[] = {"CMCM", "CDCD", "IVIV", "IXIX", "XLXL"; i=0; i< badvalues.length length; i++){ try { int vysl= Roman.fromRoman fromroman(badvalues badvalues[i]); catch (NumberFormatException e){ continue; fail("expected IllegalArgumentException for: "+ badvalues[i]);
public void testmalformedantecedent() { String badvalues[] = {"IIMMCC", "VX", "DCM", "CMM", "CMD", "IXIV", "MCMC", "XCX", "IVI", "LM", "LD", "LC"; int vysl; i= 0; i< badvalues.length length; i++ ){ try { vysl= Roman.fromRoman fromroman(badvalues badvalues[i]); catch catch(numberformatexception e){ continue; fail("expected IllegalArgumentException for: "+ badvalues[i]+ " (" + vysl); ; public void testsanity() { i= 0; i< 4000; i++){ int vysl = Roman.fromRoman fromroman(roman. (Roman.toRoman toroman(i)); assertequals(i, vysl);
// zaklad public static Test suite() { return new TestSuite(RomanTest RomanTest.class class); public static void main(string String[] args) { junit.textui textui.testrunner TestRunner.run(.run(suite suite());