Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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
java_weekendowa_20221008
Commits
9a3060c7
Commit
9a3060c7
authored
Dec 11, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
JUnit 4
parent
30b7cf0d
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
174 additions
and
0 deletions
+174
-0
pom.xml
PC40-Testy/pom.xml
+6
-0
KontoTest.java
PC40-Testy/src/test/java/junit4/KontoTest.java
+85
-0
Test4_JUnit.java
PC40-Testy/src/test/java/junit4/Test4_JUnit.java
+40
-0
Test5_Parametry.java
PC40-Testy/src/test/java/junit4/Test5_Parametry.java
+43
-0
No files found.
PC40-Testy/pom.xml
View file @
9a3060c7
...
@@ -12,6 +12,12 @@
...
@@ -12,6 +12,12 @@
<dependencies>
<dependencies>
<dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.13.2
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.junit.jupiter
</groupId>
<groupId>
org.junit.jupiter
</groupId>
<artifactId>
junit-jupiter-api
</artifactId>
<artifactId>
junit-jupiter-api
</artifactId>
<version>
5.9.1
</version>
<version>
5.9.1
</version>
...
...
PC40-Testy/src/test/java/junit4/KontoTest.java
0 → 100644
View file @
9a3060c7
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
());
}
}
PC40-Testy/src/test/java/junit4/Test4_JUnit.java
0 → 100644
View file @
9a3060c7
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
));
}
}
PC40-Testy/src/test/java/junit4/Test5_Parametry.java
0 → 100644
View file @
9a3060c7
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
);
}
}
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