In: Computer Science
I'm really confused Junit test case Here is my code.
How can I do Junit test with this code?
package pieces;
import java.util.ArrayList;
import board.Board;
public class Knight extends Piece {
public Knight(int positionX, int positionY, boolean
isWhite) {
super("N", positionX, positionY,
isWhite);
}
@Override
public String getPossibleMoves() {
ArrayList<String>
possibleMoves = new ArrayList<>();
// check if squares where knight
can go are available for it
if(positionX - 2 >= 0 &&
positionY - 1 >= 0) {
if(Board.board[positionX - 2][positionY - 1] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX - 2,
positionY - 1));
}else {
if(Board.board[positionX - 2][positionY -
1].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX - 2,
positionY - 1));
}
}
}
if(positionX - 2 >= 0 &&
positionY + 1 < 8) {
if(Board.board[positionX - 2][positionY + 1] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX - 2,
positionY + 1));
}else {
if(Board.board[positionX - 2][positionY +
1].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX - 2,
positionY + 1));
}
}
}
if(positionX + 2 < 8 &&
positionY - 1 >= 0) {
if(Board.board[positionX + 2][positionY - 1] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX + 2,
positionY - 1));
}else {
if(Board.board[positionX + 2][positionY -
1].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX + 2,
positionY - 1));
}
}
}
if(positionX + 2 < 8 &&
positionY + 1 < 8) {
if(Board.board[positionX + 2][positionY + 1] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX + 2,
positionY + 1));
}else {
if(Board.board[positionX + 2][positionY +
1].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX + 2,
positionY + 1));
}
}
}
if(positionX - 1 >= 0 &&
positionY - 2 >= 0) {
if(Board.board[positionX - 1][positionY - 2] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX - 1,
positionY - 2));
}else {
if(Board.board[positionX - 1][positionY -
2].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX - 1,
positionY - 2));
}
}
}
if(positionX - 1 >= 0 &&
positionY + 2 < 8) {
if(Board.board[positionX - 1][positionY +2] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX - 1,
positionY + 2));
}else {
if(Board.board[positionX - 1][positionY +
2].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX - 1,
positionY + 2));
}
}
}
if(positionX + 1 < 8 &&
positionY - 2 >= 0) {
if(Board.board[positionX + 1][positionY - 2] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX + 1,
positionY - 2));
}else {
if(Board.board[positionX + 1][positionY -
2].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX + 1,
positionY - 2));
}
}
}
if(positionX + 1 < 8 &&
positionY + 2 < 8) {
if(Board.board[positionX + 1][positionY + 2] == null) {
possibleMoves.add(Board.positionToChessPosition(positionX + 1,
positionY + 2));
}else {
if(Board.board[positionX + 1][positionY +
2].isWhite() ^ isWhite) {
possibleMoves.add(Board.positionToChessPosition(positionX + 1,
positionY + 2));
}
}
}
String result = "";
if(!possibleMoves.isEmpty())
{
result +=
possibleMoves.get(0);
for(int i = 1; i
< possibleMoves.size(); i++) {
result += ", " + possibleMoves.get(i);
}
}
return result;
}
@Override
public String toString() {
return "N" +
Board.positionToChessPosition(positionX, positionY);
}
}
public abstract class Piece {
protected String name;
protected int positionX, positionY;
public boolean isWhite;
/**
* Constructor for piece
* @param positionX position x on the board
* @param positionY position y on the board
* @param isWhite white or black piece
*/
public Piece(String name, int positionX, int positionY, boolean isWhite) {
this.name = name;
this.positionX = positionX;
this.positionY = positionY;
this.isWhite = isWhite;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPositionX() {
return positionX;
}
public void setPositionX(int positionX) {
this.positionX = positionX;
}
public int getPositionY() {
return positionY;
}
public void setPositionY(int positionY) {
this.positionY = positionY;
}
public boolean isWhite() {
return isWhite;
}
public void setWhite(boolean isWhite) {
this.isWhite = isWhite;
}
/**
* This method will return string that is all legal moves for this piece
* @return string of chess squares on the board where piece can move
*/
public abstract String getPossibleMoves();
}
JUnit test
PieceTest.java
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class PieceTest {
// static reference of class Piece
static Piece piece;
@BeforeClass
public static void setUp() {
// instantiate class Knight
piece=new Knight(1, 2, true);
}
// test case to assert getName() method
@Test
public void testGetName() {
assertEquals("N",
piece.getName());
}
// test case to assert getName() method
@Test
public void testGetName2() {
assertNotEquals("K",
piece.getName());
}
// test case to assert getPositionX() method
@Test
public void testGetPositionX() {
assertEquals(1,
piece.getPositionX());
}
// test case to assert getPositionY() method
@Test
public void testGetPositionY() {
assertNotEquals(2,
piece.getPositionY());
}
@Test
// test case to assert isWhite() method
public void testIsWhite() {
assertTrue(piece.isWhite());
}
@AfterClass
public static void release() {
piece=null; // release object
memory
}
}
KnightTest.java
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class KnightTest {
// static reference of class Knight
static Knight knight;
@BeforeClass
public static void setUp() {
// instantiate class Knight
knight=new Knight(5, 5,
true);
}
// test case to assert getPossibleMoves() method
@Test
public void testGetPossibleMoves() {
assertEquals("E5",
knight.getPossibleMoves());
}
@AfterClass
public static void release() {
knight=null; // release object
memory
}
}
Output Sample