diff --git a/schach/pom.xml b/schach/pom.xml index 8f7d095..c46a7b8 100644 --- a/schach/pom.xml +++ b/schach/pom.xml @@ -40,6 +40,14 @@ 3.8.1 test + + + org.mockito + mockito-core + 5.11.0 + test + + diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java index b59d3fe..b4287bc 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java @@ -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); diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java index 90be5aa..574f85d 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java @@ -115,4 +115,9 @@ public class ChessEngine { return board.getSideToMove().toString(); // "WHITE" oder "BLACK" } + public void setPositionFromFEN(String fen) { + board.loadFromFen(fen); + } + + } diff --git a/schach/src/test/java/de/hs_mannheim/informatik/chess/test/ChessTest.java b/schach/src/test/java/de/hs_mannheim/informatik/chess/test/ChessTest.java deleted file mode 100644 index ae4eadb..0000000 --- a/schach/src/test/java/de/hs_mannheim/informatik/chess/test/ChessTest.java +++ /dev/null @@ -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"); - } - -} diff --git a/schach/src/test/java/de/hs_mannheim/informatik/chess/test/ControllerTest.java b/schach/src/test/java/de/hs_mannheim/informatik/chess/test/ControllerTest.java new file mode 100644 index 0000000..36b2b34 --- /dev/null +++ b/schach/src/test/java/de/hs_mannheim/informatik/chess/test/ControllerTest.java @@ -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()); + } + + + + +}