Skip to content

Commit

Permalink
ActionHandler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bertalanaron committed May 16, 2024
1 parent 5d58921 commit ac187d3
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 6 deletions.
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

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

<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand All @@ -30,6 +31,7 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
Expand Down Expand Up @@ -85,8 +87,9 @@

<!-- 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>
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
170 changes: 168 additions & 2 deletions src/test/java/ActionHandlerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,171 @@
import static org.junit.jupiter.api.Assertions.*;
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);
}

@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;
}
}
}
22 changes: 22 additions & 0 deletions src/test/java/BlankFieldTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class BlankFieldTest {
private BlankField field;

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

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

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

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


}
67 changes: 67 additions & 0 deletions src/test/java/FixerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

class FixerTest {
private Fixer fixer;

@BeforeEach
void init() {
fixer = new Fixer("fixer", "picture");
}

@Test
void Fix() {
Active active = Mockito.mock(Active.class);

fixer.Fix(active);

verify(active, times(1)).Fix();
}

@Test
void RemoveActivePipe() {
Pipe pipe = Mockito.mock(Pipe.class);

fixer.RemoveActivePipe(pipe);

verify(pipe, times(1)).Remove(fixer);
}

@Test
void CarryPump() {
Tank tank = Mockito.mock(Tank.class);

fixer.CarryPump(tank);

verify(tank, times(1)).GivePump(fixer);
}

@Test
void CarryPipe() {
Tank tank = Mockito.mock(Tank.class);

fixer.CarryPipe(tank);

verify(tank, times(1)).GivePipe(fixer);
}

@Test
void ActiveGetSet() {
Active active = Mockito.mock(Active.class);

fixer.SetActive(active);

assertEquals(active, fixer.GetActive());
}

@Test
void HasActiveGetSet() {
boolean hasActive = true;

fixer.SetHasActive(hasActive);

assertEquals(hasActive, fixer.GetHasActive());
}

}
15 changes: 15 additions & 0 deletions src/test/java/FountainTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class FountainTest {
private Fountain fountain;

@BeforeEach
void init() {
this.fountain = new Fountain();
}

@Test
void GetReferenceID() {
this.fountain = new Fountain("ref");
assertEquals("ref", fountain.GetReferenceID());
}

}
36 changes: 36 additions & 0 deletions src/test/java/GameTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class GameTest {
private Game game;

@BeforeEach
void init() {
this.game = Game.Get();
}

@Test
void ResetPlayers() {
game.ResetPlayers();

// Player player1 = Mockito.mock(Player.class);
// Player player2 = Mockito.mock(Player.class);
// ArrayList<Player> players = new ArrayList<>();
// players.add(player1);
// players.add(player2);

// this.game = Mockito.spy(Game.class);

// when(game.getPlayerCount())
// .thenReturn(2);

// game.getPlayerCount();


// this.game = Game.Get();
// game.SetPlayers(players);
}

}

0 comments on commit ac187d3

Please sign in to comment.