Commit 9a3060c7 by Patryk Czarnik

JUnit 4

parent 30b7cf0d
......@@ -12,6 +12,12 @@
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
......
package junit4;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import konto.Konto;
public class KontoTest {
private Konto konto;
@Before
public void setUp() {
konto = new Konto(1, "Ala", 1000);
}
@After
public void tearDown() {
// System.out.println("koniec testu");
}
@Test
public void testToString() {
assertEquals("Konto nr 1, wł. Ala, saldo: 1000", konto.toString());
}
@Test
public void testWplata() {
konto.wplata(400);
assertEquals(1400, konto.getSaldo());
}
// Poniżej testy określające co ma się stać w przypadku błędnych danych - oczekiwane są wyjątki.
// Sprawdzanie wyjątków w JUnit 4.
// Dopisując expected do @Test mówimy, że test powinien skończyć się podanym wyjątkiem.
// Jeśli jest taki wyjątek - OK.
// Jeśli nie ma żadnego lub jest inny - porażka testu.
@Test(expected = IllegalArgumentException.class)
public void testWplataUjemna() {
konto.wplata(-100);
}
// Ograniczneia tego podejścia:
// 1) nie ma jak sprawdzić szczegółów samego wyjątku, np. jaki jest message
// 2) gdyby ktoś chciał sprawdzić jaki jest stan końcowy obiektu,
// to bardzo łatwo może popełnić błąd logiczny i napisać asercję za miejscem, w którym wystepuje wyjątek
@Test(expected = IllegalArgumentException.class)
public void testWplataUjemna_v2() {
konto.wplata(-100);
// tutaj nie pisze się asercji, bo gdy nastąpi wyjątek, to nie dojdziemy do tego miejsca w kodzie
// TAKA ASERCJA SIĘ NIE WYKONA
assertEquals(1000, konto.getSaldo());
}
// Aby to naprawić, można użyć finally:
@Test(expected = IllegalArgumentException.class)
public void testWplataUjemna_v3() {
try {
konto.wplata(-100);
} finally {
assertEquals(1000, konto.getSaldo());
}
}
// Nawet nie używając specjalnych operacji z biblioteki do testowania,
// możemy opisać oczekiwanie, że ma być wyjątek
@Test
public void testWplataUjemnaTryCatch() {
try {
konto.wplata(-100);
fail("Powinien być wyjątek, a nie było wyjątku");
} catch (IllegalArgumentException e) {
// tu ewentualnie assert na temat wyjątku
assertEquals("Ujemna kwota w wplata", e.getMessage());
}
// upewniamy się, że po wszystkim stan konta się nie zmienił
assertEquals(1000, konto.getSaldo());
}
}
package junit4;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import funkcje.Funkcje;
public class Test4_JUnit {
@Test
public void test0() {
long wynik = Funkcje.fib(0);
assertEquals(0, wynik);
}
@Test
public void test1() {
long wynik = Funkcje.fib(1);
assertEquals(1, wynik);
}
@Test
public void test4() {
long wynik = Funkcje.fib(4);
assertEquals(3, wynik);
}
@Test
public void test10() {
assertEquals(55, Funkcje.fib(10));
}
// ograniczamy czas działania do 2s
// jeśli wykonanie trwa dłużej, to JUnit uznaje to za błąd testu
@Test(timeout = 2000)
public void test45() {
assertEquals(1134903170, Funkcje.fib(45));
}
}
package junit4;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import funkcje.Funkcje;
@RunWith(Parameterized.class)
public class Test5_Parametry {
private int arg;
private long expectedResult;
public Test5_Parametry(Integer arg, Long expectedResult) {
this.arg = arg;
this.expectedResult = expectedResult;
}
@Parameters
public static List<Object[]> parametry() {
Object[][] dane = {
{0, 0L},
{1, 1L},
{2, 1L},
{5, 5L},
{10, 55L},
};
return List.of(dane);
}
@Test
public void test() {
long result = Funkcje.fib(arg);
assertEquals(expectedResult, result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment