Я пытаюсь создать тестовый набор для моей игры. Но я не знаю, с чего начать и как протестировать весь этот класс с помощью mockito. Может ли кто-нибудь помочь мне с этим?
Сначала я издевался над создателями доски и уровня, затем я провел некоторый тест, чтобы разобрать карту с врагами, стенами и игроком на ней, а затем вызвал анализатор, чтобы проанализировать карту, а затем убедиться, что создатель доски и уровня методы были вызваны, но я не знаю, как продолжить отсюда. Также у меня есть некоторые проблемы с издевательством над классом Board, потому что у его конструктора нет модификатора доступа, поэтому я не могу получить к нему доступ, когда мой тестовый класс не входит в тот же пакет (как я могу обойти эту проблему).
Вот класс анализатора, который я хочу протестировать:
public class Parser {
private LevelMaker levelMaker;
private BoardMaker boardMaker;
public Parser(LevelMaker levelMaker, BoardMaker boardMaker) {
this.levelMaker = levelMaker;
this.boardMaker = boardMaker;
}
/**
* Makes Board on a level from txt file of a map.
*
* Supported characters:
* ' ' an empty square.
* '#' a wall.
* '.' coins to collect.
* 'P' square for players.
* 'G' a square with a gamer (enemy in the game).
*
* @param map The text representation of the board
* @return The level as represented by text map.
*/
public Level parsingTheMap(char[][] map) {
Square[][] grid = new Square[map.length][map[0].length];
List<Enemy> enemies = new ArrayList<>();
List<Square> startPositions = new ArrayList<>();
makeGrid(map, map.length, map[0].length, grid, enemies, startPositions);
Board board = boardMaker.createBoardFromGrid(grid);
Level theLevel = levelMaker.createLevel(board, enemies, startPositions);
return theLevel;
}
private void makeGrid(char[][] map, int width, int height,
Square[][] grid, List<Enemy> enemies, List<Square> startPositions) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
char c = map[x][y];
addSquare(grid, enemies, startPositions, x, y, c);
}
}
}
/**
* Adds a square to the grid based on a given character. These
* character come from the map files and describe the type
* of square.
*
* @param grid The grid of squares with board[x][y] being the
* square at column x, row y.
* @param enemies List of all enemies that were added to the map.
* @param startPositions List of all start positions that were added
* to the map.
* @param x coordinate of the square.
* @param y coordinate of the square.
* @param c Character describing the square type.
*/
protected void addSquare(Square[][] grid, List<Enemy> enemies,
List<Square> startPositions, int x, int y, char c) {
switch (c) {
case ' ':
grid[x][y] = boardMaker.createEmptySquare();
break;
case '#':
grid[x][y] = boardMaker.createWall();
break;
case '.':
Square coinsSquare = boardMaker.createEmptySquare();
grid[x][y] = coinsSquare;
levelMaker.createCoin().occupying(coinsSquare);
break;
case 'G':
Square enemySquare = makeEnemySquare(enemies, levelMaker.createEnemy());
grid[x][y] = enemySquare;
break;
case 'P':
Square playerSquare = boardMaker.createEmptySquare();
grid[x][y] = playerSquare;
startPositions.add(playerSquare);
break;
default:
throw new GameConfigurationException("Invalid character at "
+ x + "," + y + ": " + c);
}
}
/**
* creates a Square with the specified enemy on it
* and appends the placed enemy into the enemy list.
*
* @param enemies all the enemies in the level
* @param enemy the newly created enemy to be placed
* @return a square with the enemy on it.
*/
protected Square makeEnemySquare(List<Enemy> enemies, Enemy enemy) {
Square enemySquare = boardMaker.createEmptySquare();
enemies.add(enemy);
enemy.occupying(enemySquare);
return enemySquare;
}
/**
* Parses the list of strings into a 2-dimensional character array and
* passes it on to parsingTheMap(char[][])
*
* @param text The plain text, with every entry in the list being a equally
* sized row of squares on the board and the first element being
* the top row.
* @return The level as represented by the text.
*/
public Level parsingTheMap(List<String> text) {
int height = text.size();
int width = text.get(0).length();
char[][] map = new char[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
map[x][y] = text.get(y).charAt(x);
}
}
return parsingTheMap(map);
}
/**
* Parses the provided input stream as a character stream and passes it
* result to parsingTheMap(List).
*
* @param src The input stream that will be read.
* @return The parsed level as represented by the text on the input stream.
* @throws IOException
*/
public Level parsingTheMap(InputStream src) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
src, "UTF-8"))) {
List<String> linesOfMap = new ArrayList<>();
while (reader.ready()) {
lines.add(reader.readLine());
}
return parsingTheMap(linesOfMap);
}
}
/**
* Parses the provided input stream as a character stream and passes it
* result to parsingTheMap(List).
*
* @param map Name of a resource that will be read.
* @return The parsed level as represented by the text on the input stream.
* @throws IOException
*/
public Level parsingTheMap(String map) throws IOException {
try (InputStream boardStream = parsingTheMap.class.getResourceAsStream(map)) {
if (boardStream == null) {
throw new GameConfigurationException("Something went wrong: " + map);
}
return parsingTheMap(boardStream);
}
}
protected BoardMaker getBoardMaker() {
return boardMaker;
}
}
Мой тест пока:
public class ParserTest {
private BoardMaker boardMaker;
private LevelMaker levelMaker;
private Parser parser;
@BeforeEach
void setUp() {
levelMaker = mock(LevelMaker.class);
boardMaker = mock(BoardMaker.class);
parser = new Parser(levelMaker, boardMaker);
}
@Test
void parserTest() {
List<String> map = Lists.newArrayList(
"##########",
"#........#",
"#P G#");
Square square = new Square();
Board board = mock(Board.class);
Level level = mock(Level.class);
Coin coin = mock(Coin.class);
when(boardMaker.createWall()).thenReturn(square);
when(boardMaker.createEmptySquare()).thenReturn(square);
when(levelMaker.createCoin()).thenReturn(coin);
when(boardMaker.createBoard(any(Square[][].class))).thenReturn(board);
when(levelMaker.createLevel(any(Board.class), anyList(), anyList()))
.thenReturn(level);
//the level to compare with returned level from mapParser
Level Level = parser.parsingTheMap(map);
verify(boardMaker).createBoard(any(Square[][].class));
verify(boardMaker).createEmptySquare();
verify(boardMaker).createWall();
verify(levelMaker).createLevel(any(Board.class), anyList(), anyList());
verify(levelMaker).createCoin().occupying(any(Square.class));
}
}