Difference between revisions of "Java - Unit Testing"

From Coders.Bay Wiki
Jump to navigation Jump to search
Line 4: Line 4:


In order for the Unit Tests to work, you need to add JUnit5 to the class path. When you hover over the @Test annotation in the test class and press Alt+Einfg you should get a suggestion that provides the Add JUnit5 to classpath functionality.
In order for the Unit Tests to work, you need to add JUnit5 to the class path. When you hover over the @Test annotation in the test class and press Alt+Einfg you should get a suggestion that provides the Add JUnit5 to classpath functionality.
Goal
Goal: In the end an examplaray TicTacToe game should look like this:
 
In the end an examplaray TicTacToe game should look like this:


Welcome to TicTacToe
Welcome to TicTacToe

Revision as of 08:48, 20 January 2022

Tag 1

JUnit

Aufgabe: Tic Tac Toe

In order for the Unit Tests to work, you need to add JUnit5 to the class path. When you hover over the @Test annotation in the test class and press Alt+Einfg you should get a suggestion that provides the Add JUnit5 to classpath functionality. Goal: In the end an examplaray TicTacToe game should look like this:

Welcome to TicTacToe Player 1 choose your field [1-9] 5

| | 
|X| 
| | 

Player 2 choose your field [1-9] 1

O| | 
 |X| 
 | | 

Player 1 choose your field [1-9] 3

O| |X
 |X| 
 | | 

Player 2 choose your field [1-9] 7

O| |X
 |X| 
O| | 

Player 1 choose your field [1-9] 4

O| |X
X|X| 
O| | 

Player 2 choose your field [1-9] 6

O| |X
X|X|O
O| | 

Player 1 choose your field [1-9] 8

O| |X
X|X|O
O|X| 

Player 2 choose your field [1-9] 2

O|O|X
X|X|O
O|X| 

Player 1 choose your field [1-9] 9

O|O|X
X|X|O
O|X|X

The game is over. It' a draw.

Or another game with an early win:

Welcome to TicTacToe Player 1 choose your field [1-9] 1

X| | 
 | | 
 | | 

Player 2 choose your field [1-9] 5

X| | 
 |O| 
 | | 

Player 1 choose your field [1-9] 2

X|X| 
 |O| 
 | | 

Player 2 choose your field [1-9] 4

X|X| 
O|O| 
 | | 

Player 1 choose your field [1-9] 3

X|X|X
O|O| 
 | | 

The game is over. Player 1 won!

Tag 2

Aufgabe: Game of Life Unit Testen

Erweitere dein Game of Life um Unit Tests. https://wiki.streampy.at/index.php?title=Java_-_Methoden#Aufgabe:_Game_of_Life

Kompetenzcheck

Aufgabe: Sum of Two mit Tests

Gegeben sind zwei int-Arrays a und b, mit beliebigen Werten und ein Zielwert (target value) v. Finde heraus, ob es ein Nummernpaar gibt, wobei ein Wert aus a und der andere Wert aus b sein muss, die in Summe den Zielwert v ergeben. Es soll true zurückgegeben werden, falls es so ein Paar gibt, andernfalls false.

Schreibe die Methode sumOfTwo(int[] a, int[] b, int v) wie oben beschrieben. Die Lösung soll möglichst effizient sein, also kein!! Bruteforce.

Hier folgen zwei Beispiele für das oben beschriebene Problem:

int[] a = [1,2,3];
int[] b = [10,20,30,40];
int v = 42;

Die Methode sumOfTwo gibt true zurück, da 40 + 2 = 42 ist. a und b müssen nicht sortiert sein und können auch mehrmals den gleichen Wert beinhalten.

int[] a = {0,0,-5,30212};
int[] b = {-10,40,-3,9};
int v = -8;

Es wird wieder true returned, da −5 + (−3) = −8 ist. Wichtig: Denke daran, dass Arrays in Java 231 − 1 Elemente enthalten können

  • kein Bruteforce.
  • HashSets sind hilfreich, da sie einfügen und lesen in effizienter und vor allem

konstanter Zeit ermöglichen. https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/ util/HashSet.html

// HashSet für int - Werte
HashSet<Integer> hashSet = new HashSet<>();

Mit Bruteforce ist gemeint, dass jedes einzelne Element aus a mit jedem einzelnen Element aus b addiert wird und danach mit v verglichen wird. Hier ein Beispiel:

a = [1, 2, 3]
b = [4, 5, 6]
v = 7
a[0] = 1
b[0] + a[0] = 7?
.
.
b[3] + a[0] = 7
return true