Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_java_alx_20240321
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
kurs_java_alx_20240321
Commits
2ae2b190
Commit
2ae2b190
authored
Apr 12, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dodatkowe testy dot wyjątków
parent
15c00c5f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
1 deletions
+68
-1
KontoTest.java
src/p11_klasy/enkapsulacja/KontoTest.java
+68
-1
No files found.
src/p11_klasy/enkapsulacja/KontoTest.java
View file @
2ae2b190
package
p11_klasy
.
enkapsulacja
;
package
p11_klasy
.
enkapsulacja
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
*
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
...
@@ -30,4 +30,71 @@ class KontoTest {
...
@@ -30,4 +30,71 @@ class KontoTest {
assertEquals
(
800
,
konto
.
getSaldo
());
assertEquals
(
800
,
konto
.
getSaldo
());
}
}
// Temat sprawdzania wyjątków w testach.
// Aby stwierdzić, że w pewnej sytuacji metoda POWINNA skończyć się wyjątkiem,
// możemy użyć odpowiednio try-catch (testWplataTry)
// albo użyć dedykowanych rozwiązań JUnit - dalsze przykłady
@Test
void
testWplataTry
()
{
try
{
konto
.
wplata
(-
100
);
// Gdyby nie było wyjątku i program doszedłby do tego miejsca,
// test powinien skończyć się porażką
fail
(
"Powinien być wyjątek, a nie ma."
);
}
catch
(
IllegalArgumentException
e
)
{
// Jeśli wyłapiemy wyjątek, możemy sprawdzić w teście jego szczegóły,
// np. jaki jest message (bardzo rzadko takie rzeczy sprawdza się w testach, ale można):
assertEquals
(
"Kwota nie jest dodatnia"
,
e
.
getMessage
());
}
finally
{
// Możemy też upewnić się, że w przypadku wystąpienia wyjątku stan konta się nie zmienił.
assertEquals
(
1000
,
konto
.
getSaldo
());
}
}
// Analogiczny test można też napisać wykorzystując nową funkcjonalność JUnit 5:
@Test
void
testWplataUjemna_v1
()
{
// assertThrows wykonuje podany fragment kodu (w praktyce podaje się wyrażenie lambda)
// i sprawdza czy ten kończy się podanym rodzajem wyjątku.
// Jeśli jest wyjątek - dobrze
// Jeśli nie ma wyjątku - test failuje
assertThrows
(
IllegalArgumentException
.
class
,
()
->
{
konto
.
wplata
(-
100
);
});
// Dodatkowo po wykonaniu assertThrows możemy sprawdzić jaki jest stan końcowy,
// np. czy saldo się nie zmieniło.
assertEquals
(
1000
,
konto
.
getSaldo
());
}
// Aby sprawdzić jaki jest message w wyjątku itp, możemy odebrać obiekt wyjątku i sprawdzić bezpośrednio
@Test
void
testWplataUjemna_v2
()
{
IllegalArgumentException
exception
=
assertThrows
(
IllegalArgumentException
.
class
,
()
->
{
konto
.
wplata
(-
100
);
});
assertEquals
(
"Kwota nie jest dodatnia"
,
exception
.
getMessage
());
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
());
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment