Dion Aliu
3 years ago
4 changed files with 64 additions and 31 deletions
-
21src/main/java/Game/Game.java
-
14src/main/java/Game/Tictactoe.java
-
43src/test/java/Game/GameTest.java
-
17src/test/java/Game/TictactoeTest.java
@ -1,7 +1,24 @@ |
|||||
package Game; |
package Game; |
||||
|
|
||||
public interface Game { |
|
||||
public abstract void print(); |
|
||||
|
import java.util.ArrayList; |
||||
|
|
||||
|
public abstract class Game { |
||||
|
|
||||
|
protected ArrayList<String> outputBuffer = new ArrayList<>(); |
||||
|
|
||||
|
public void print() { |
||||
|
for (String s : outputBuffer) { |
||||
|
System.out.println(s); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected void setOutputBuffer(ArrayList<String> outputBuffer) { |
||||
|
this.outputBuffer = outputBuffer; |
||||
|
} |
||||
|
|
||||
|
public ArrayList<String> getOutputBuffer() { |
||||
|
return this.outputBuffer; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
@ -0,0 +1,43 @@ |
|||||
|
package Game; |
||||
|
|
||||
|
import org.junit.jupiter.api.AfterEach; |
||||
|
import org.junit.jupiter.api.BeforeEach; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.mockito.Mockito; |
||||
|
|
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.PrintStream; |
||||
|
import java.util.ArrayList; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
import static org.mockito.Mockito.mock; |
||||
|
|
||||
|
class GameTest { |
||||
|
|
||||
|
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
||||
|
private final PrintStream originalOut = System.out; |
||||
|
|
||||
|
Game game; |
||||
|
|
||||
|
@BeforeEach |
||||
|
void setUp() { |
||||
|
System.setOut(new PrintStream(outContent)); |
||||
|
game = mock(Game.class, Mockito.CALLS_REAL_METHODS); |
||||
|
} |
||||
|
|
||||
|
@AfterEach |
||||
|
void tearDown() { |
||||
|
System.setOut(originalOut); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void print() { |
||||
|
ArrayList<String> testOB = new ArrayList<>(); |
||||
|
testOB.add("Hello"); |
||||
|
testOB.add("World"); |
||||
|
testOB.add("!!!"); |
||||
|
game.setOutputBuffer(testOB); |
||||
|
game.print(); |
||||
|
assertEquals("Hello\nWorld\n!!!\n", outContent.toString().replaceAll("\r", "")); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue