Commit 5a0a5e76 by Patryk Czarnik

Testy JUnit i nowe metody w Konto

parent 6d176abe
...@@ -14,4 +14,13 @@ ...@@ -14,4 +14,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.14.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>
...@@ -34,4 +34,32 @@ public class Konto { ...@@ -34,4 +34,32 @@ public class Konto {
public String toString() { public String toString() {
return "Konto nr " + numer + ", saldo " + saldo + ", wł. " + wlasciciel; return "Konto nr " + numer + ", saldo " + saldo + ", wł. " + wlasciciel;
} }
public void wplata(int kwota) {
if(kwota <= 0) {
throw new IllegalArgumentException("kwota wpłaty nie jest dodatnia");
}
saldo += kwota;
}
public void wyplata(int kwota) throws BrakSrodkow {
if(kwota <= 0) {
throw new IllegalArgumentException("kwota wypłaty nie jest dodatnia");
}
if(kwota > saldo) {
throw new BrakSrodkow("Za mało kasy");
}
saldo -= kwota;
}
public void przelew(Konto inne, int kwota) throws BrakSrodkow {
if(kwota <= 0) {
throw new IllegalArgumentException("kwota przelewu nie jest dodatnia");
}
if(kwota > saldo) {
throw new BrakSrodkow("Za mało kasy");
}
this.saldo -= kwota;
inne.saldo += kwota;
}
} }
package com.example;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class KontoTest {
static final String osoba = "Ala", osoba2 = "Ola";
Konto konto;
@BeforeEach
void setUp() throws Exception {
konto = new Konto(1, 1000, osoba);
}
@Test
void testKonstruktorIGettery() {
assertEquals(1, konto.getNumer());
assertEquals(osoba, konto.getWlasciciel());
assertEquals(1000, konto.getSaldo());
}
@Test
void testToString() {
String napis = konto.toString();
assertEquals("Konto nr 1, saldo 1000, wł. "+osoba.toString(), napis);
}
@Test
void testWplata() {
konto.wplata(400);
assertEquals(1400, konto.getSaldo());
}
@Test
void testWyplata() throws BrakSrodkow {
konto.wyplata(300);
assertEquals(700, konto.getSaldo());
}
@Test
void testWplataUjemna() {
assertThrows(IllegalArgumentException.class, () -> {
konto.wplata(-100);
});
assertEquals(1000, konto.getSaldo());
}
@Test
void testWyplataUjemna() {
assertThrows(IllegalArgumentException.class, () -> {
konto.wyplata(-100);
});
assertEquals(1000, konto.getSaldo());
}
@Test
void testBrakSrodkow() {
assertThrows(BrakSrodkow.class, () -> {
konto.wyplata(1300);
});
assertEquals(1000, konto.getSaldo());
}
@Test
void testPrzelew() throws BrakSrodkow {
Konto inne = new Konto(2, 2000, osoba2);
konto.przelew(inne, 400);
assertEquals(600, konto.getSaldo());
assertEquals(2400, inne.getSaldo());
}
@Test
void testPrzelewUjemny() {
Konto inne = new Konto(2, 2000, osoba2);
assertThrows(IllegalArgumentException.class, () -> {
konto.przelew(inne, -100);
});
assertEquals(1000, konto.getSaldo());
assertEquals(2000, inne.getSaldo());
}
}
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