Tests fast vollständig

GitBranch
valen 2025-06-22 15:07:06 +02:00
parent 4c58eebc65
commit 9e095418a2
5 changed files with 89 additions and 35 deletions

View File

@ -40,6 +40,14 @@
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Mockito damit man Controller und Gui besser testen kann-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -101,7 +101,7 @@ public class Controller {
for (int[] xy : highlightedFields) {
resetFieldBackground(xy[0], xy[1]);
}
highlightedFields.clear();
highlightedFields.clear();
int oldGuiRow = gui.isFlipped() ? 7 - selectedRow : selectedRow;
int oldGuiCol = gui.isFlipped() ? 7 - selectedCol : selectedCol;
gui.getField(oldGuiRow, oldGuiCol).setBorder(null);

View File

@ -115,4 +115,9 @@ public class ChessEngine {
return board.getSideToMove().toString(); // "WHITE" oder "BLACK"
}
public void setPositionFromFEN(String fen) {
board.loadFromFen(fen);
}
}

View File

@ -1,34 +0,0 @@
package de.hs_mannheim.informatik.chess.test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ChessTest {
@BeforeAll
static void setUpBeforeClass() throws Exception {
}
@AfterAll
static void tearDownAfterClass() throws Exception {
}
@BeforeEach
void setUp() throws Exception {
}
@AfterEach
void tearDown() throws Exception {
}
@Test
void test() {
fail("Not yet implemented");
}
}

View File

@ -0,0 +1,75 @@
package de.hs_mannheim.informatik.chess.test;
import static org.junit.jupiter.api.Assertions.*;
import de.hs_mannheim.informatik.chess.controller.Controller;
import de.hs_mannheim.informatik.chess.model.*;
import de.hs_mannheim.informatik.chess.view.GameGui;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;
import java.util.List;
import javax.swing.JLabel;
public class ControllerTest {
private Controller controller;
private ChessEngine engineMock;
private GameGui guiMock;
@BeforeEach
public void setUp() {
engineMock = mock(ChessEngine.class);
guiMock = mock(GameGui.class);
// Simuliere 8x8 JLabel-Felder
JLabel[][] fakeFields = new JLabel[8][8];
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
fakeFields[row][col] = new JLabel();
when(guiMock.getField(row, col)).thenReturn(fakeFields[row][col]);
}
}
// Simuliere Flip-Button (falls verwendet)
when(guiMock.getFlipBoardButton()).thenReturn(new javax.swing.JButton());
// Jetzt ist der Controller sicher
controller = new Controller(guiMock, engineMock);
}
@Test //Testet dass keine Fehlermeldung bei legalen Move kommt
public void reagiertRichtigAufMove_WennMoveErlaubt() {
MoveDTO move = new MoveDTO(1, 1, 2, 2);
when(engineMock.move(move)).thenReturn(true);
when(engineMock.getMoveListStringsGrouped()).thenReturn(List.of("1. e4 e5"));
controller.handleMove(move);
verify(guiMock).updateMoveList(List.of("1. e4 e5"));
verify(guiMock, never()).displayMessage("Ungültiger Zug!");
}
@Test //Testet dass keine Fehlermeldung bei legalen Move kommt
public void reagiertRichtigAufMove_WennMoveNichtErlaubt() {
MoveDTO move = new MoveDTO(1, 1, 2, 2);
when(engineMock.move(move)).thenReturn(false);
controller.handleMove(move);
verify(guiMock).displayMessage("Ungültiger Zug!");
verify(guiMock, never()).updateMoveList(any());
}
}