Skip to content

Commit

Permalink
Merge pull request #11 from BME-MIT-IET/unit-tests
Browse files Browse the repository at this point in the history
Unit tesztek
  • Loading branch information
leventebajak authored May 21, 2024
2 parents 506d050 + b80d61a commit 613d270
Show file tree
Hide file tree
Showing 11 changed files with 1,410 additions and 4 deletions.
Binary file added doc/Coverage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Egységtesztek készítése + tesztek kódlefedettségének mérése

## Egységtesztek készítése

Az egységtesztek készítéséhez a **JUnit** és **Mockito** package-eket használtuk.

Nem minden osztály lett tesztelve, de azok, amelyeket teszteltünk, azok részletesen le lettek fedve.

Az egységtesztek a [`/src/test/java`](../src/test/java) mappában találhatóak.

A UI osztályok tesztelése nem volt célunk, mivel ezt a **_Manuális tesztek megtervezése_** feladat magában foglalja.

## Kódlefedettség mérése

A kódlefedettséget a **JaCoCo** segítségével mértük. A build konfigurációban be lett állítva, hogy a tesztek futtatása
után a JaCoCo generáljon egy jelentést.

Az elkészült jelentés a `/target/site/jacoco/index.html` fájl megnyitásával tekinthető meg.

A jelentések elemzése segített a le nem fedett ágak azonosításában.

Több fájl lefedettsége is eléri a 100%-ot.

### Jelentés

![Code coverage](./Coverage.png)
50 changes: 48 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,36 @@
<modelVersion>4.0.0</modelVersion>

<version>1.0-SNAPSHOT</version>
<groupId>hu.main</groupId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<groupId>hu.main</groupId>
<artifactId>hu.main</artifactId>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
Expand Down Expand Up @@ -64,8 +88,30 @@

<!-- JUnit 5 requires Surefire version 2.22.1 or higher -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<version>3.0.0</version>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public class Game implements Steppable {
/**
* a jatekban szereplo jatekosok listaja
*/
private final ArrayList<Player> players;
private ArrayList<Player> players;
/**
* kezeli a koroket
*/
private final ActionHandler actionHandler = new ActionHandler();
private ActionHandler actionHandler = new ActionHandler();
/**
* a jatek vegi uzenet
*/
Expand Down
178 changes: 178 additions & 0 deletions src/test/java/ActionHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import static org.mockito.Mockito.*;

class ActionHandlerTest {
private ActionHandler actionHandler;
private static MockedStatic<Game> gameClass;
private static MockedStatic<GameFrame> gameFrameClass;

@BeforeEach
public void init() {
this.actionHandler = new ActionHandler();
}

@BeforeAll
static void initOnce() {
gameClass = Mockito.mockStatic(Game.class);
gameFrameClass = mockStatic(GameFrame.class);
}

@AfterAll
static void afterAll() {
gameClass.close();
gameFrameClass.close();
}

@Test
void PlayerTurn_hasPoints() {
Player player = Mockito.mock(Player.class);

when(player.GetActionPoints())
.thenReturn(2)
.thenReturn(1)
.thenReturn(0);
actionHandler.PlayerTurn(player);

verify(player, times(2)).InteractOptions();
}

@Test
void PlayerTurn_noPoints() {
Player player = Mockito.mock(Player.class);

when(player.GetActionPoints())
.thenReturn(0);
actionHandler.PlayerTurn(player);

verify(player, times(0)).InteractOptions();
}

@Test
void HandleTurn() {
try {
Game game = mock(Game.class);
gameClass.when(Game::Get).thenReturn(game);

Player player = mock(Player.class);
ArrayList<Player> players = new ArrayList<>();
players.add(player);

when(game.GetPlayers())
.thenReturn(players);

when(game.GetActivePlayer())
.thenReturn(player);

GameFrame gameFrame = mock(GameFrame.class);
gameFrameClass.when(GameFrame::Get)
.thenReturn(gameFrame);

GameField gameField = mock(GameField.class);
when(gameFrame.GetPlayerLayer())
.thenReturn(gameField);

when(player.GetActionPoints())
.thenReturn(3)
.thenReturn(2)
.thenReturn(1)
.thenReturn(0);

Field field = mock(Field.class);
when(player.GetField())
.thenReturn(field);

Map map = mock(Map.class);
when(game.GetMap())
.thenReturn(map);

Field field2 = mock(Field.class);
ArrayList<ArrayList<Field>> mapFields1 = new ArrayList<>();
ArrayList<ArrayList<Field>> mapFields2 = new ArrayList<>();
ArrayList<Field> mapFields2Inner = new ArrayList<>();
mapFields2Inner.add(field2);
mapFields2.add(mapFields2Inner);
when(map.GetFields())
.thenReturn(mapFields1)
.thenReturn(mapFields2);

actionHandler.HandleTurn();

verify(gameField, times(1)).UpdateField(any());
verify(game, times(1)).SetActivePlayer(player);
verify(player, times(1)).SetActionPoints(3);
verify(gameFrame, times(1)).DrawField(field);
verify(gameFrame, times(1)).DrawPlayer(player);

verify(player, times(3)).InteractOptions();

verify(gameFrame, times(1)).DrawField(field2);

verify(gameFrame, times(1)).DrawPoints();
} catch(Exception e) {
throw e;
}
}

@Test
void WorldTurn() {
try {
Game game = mock(Game.class);
gameClass.when(Game::Get)
.thenReturn(game);

Map map = mock(Map.class);
when(game.GetMap())
.thenReturn(map);

Field field1 = mock(Field.class);
Field field2 = mock(Field.class);

ArrayList<ArrayList<Field>> fieldsMat = new ArrayList<>();
ArrayList<Field> fieldsRow = new ArrayList<>();
fieldsRow.add(field1);
fieldsRow.add(field2);
fieldsMat.add(fieldsRow);
when(map.GetFields())
.thenReturn(fieldsMat);

when(field1.GetPOut())
.thenReturn(field2);
when(field2.GetPIn())
.thenReturn(field1);

when(map.GetDirection(field1, field2))
.thenReturn("left");
when(map.GetDirection(field2, field1))
.thenReturn("right");

when(field1.GetBeenStepped())
.thenReturn(false).thenReturn(true);
when(field2.GetBeenStepped())
.thenReturn(false).thenReturn(true);

GameFrame gameFrame = mock(GameFrame.class);
gameFrameClass.when(GameFrame::Get).thenReturn(gameFrame);

actionHandler.WorldTurn();

verify(field1, times(1)).Step();
verify(field2, times(1)).Step();

verify(field1, times(1)).SetBeenStepped(true);
verify(field2, times(1)).SetBeenStepped(true);

verify(gameFrame, times(1)).DrawField(field1);
verify(gameFrame, times(1)).DrawField(field2);
} catch(Exception e) {
throw e;
}
}
}
96 changes: 96 additions & 0 deletions src/test/java/BlankFieldTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

class BlankFieldTest {
private BlankField blankField;
private static MockedStatic<Game> gameClass;
private static MockedStatic<GameFrame> gameFrameClass;
private static MockedStatic<Wait> waitClass;

@BeforeEach
public void init() {
this.blankField = new BlankField();
}

@BeforeAll
static void initOnce() {
gameClass = Mockito.mockStatic(Game.class);
gameFrameClass = mockStatic(GameFrame.class);
waitClass = mockStatic(Wait.class);
}

@AfterAll
static void afterAll() {
gameClass.close();
gameFrameClass.close();
waitClass.close();
}

@Test
void GetReferenceID() {
this.blankField = new BlankField("refID");

assertEquals("refID", blankField.GetReferenceID());
}

@Test
void AcceptWater() {
Field field = mock(Field.class);

Game game = mock(Game.class);
gameClass.when(Game::Get).thenReturn(game);

assertTrue(blankField.AcceptWater(field));

verify(game).InceraseSaboteurPoints();
}

@Test
void CanAcceptWater() {
assertEquals(false, blankField.CanAcceptWater());
}

@Test
void CanAcceptPlayer() {
assertEquals(false, blankField.CanAcceptPlayer());
}

@Test
void Step() {
blankField.Step();
}

@Test
void SaboteurOptions() {
Saboteur saboteur = mock(Saboteur.class);

Wait wait = mock(Wait.class);
waitClass.when(Wait::Get)
.thenReturn(wait);

blankField.SaboteurOptions(saboteur);

verify(wait).Wait();
}

@Test
void FixerOptions() {
Fixer fixer = mock(Fixer.class);

Wait wait = mock(Wait.class);
waitClass.when(Wait::Get)
.thenReturn(wait);

blankField.FixerOptions(fixer);

verify(wait).Wait();
}

}
Loading

0 comments on commit 613d270

Please sign in to comment.