Question

In: Computer Science

I'm really confused Junit test case Here is my code. How can I do Junit test...

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();

}

Solutions

Expert Solution

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


Related Solutions

Can someone explain how to get the answers step by step? I'm really confused by this...
Can someone explain how to get the answers step by step? I'm really confused by this stats question and I really need it "dummied down." A researcher has gathered information from a random sample of 178 households. For each of the following variables, construct confidence intervals to estimate the population mean. Use the 90% level. A. An average of 2.3 people resides in each household. Standard devisfikn is 0.35 B. There was an average of 2.1 television sets (s= 0.10)...
Please do it in Excel and show detailed work I'm really confused about how to put...
Please do it in Excel and show detailed work I'm really confused about how to put the Z- value on Excel. I will rate you! The crab spider, Thomisus spectabilis, sits on flowers and preys upon visiting honeybees. Do honeybees distinguish between flowers that have crab spiders and flowers that do not? To test this, Heiling et al. (2003) gave 33 bees a choice between 2 flowers: one with, and one without a crab spider. In 23 of the 33...
I'm very confused on how to complete this code. There are 5 total classes. I have...
I'm very confused on how to complete this code. There are 5 total classes. I have some written but it isn't quite working and the Band class especially is confusing me. I posted the entire thing since the band and manager class refer to the other classes' constructors and thought it would be helpful to post those as well. Person class is a base class: Private variables String firstname, String lastname, int age Constructor has the parameters String firstname, String...
Could you explain this to me? I'm really confused. I need to learn it step by...
Could you explain this to me? I'm really confused. I need to learn it step by step. Thank you so much! So this is the sample set the s ample mean is 100 and the SD is 10 and the s ample size is 500. Can you construct the 95 % confidence interval>
Well, I'm Lillian, and I'm here because I was in a really significant car accident about...
Well, I'm Lillian, and I'm here because I was in a really significant car accident about nine months ago and a couple weeks. And I guess, well, I used to really be in control of everything. I was really good at being kind of a lynch pin of my family and being able to make sure everybody got to where they needed to be. I was able to support people emotionally and get them to where they needed to be,...
I'm confused when I can use Henderson equation for pH calculation? it seems my professor use...
I'm confused when I can use Henderson equation for pH calculation? it seems my professor use it for both calculating ph of titration and for buffer solution too. but can I use this equation for both ??
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can explain why in terms of concentration gradient and the fact it's in a hypotonice solution which would cause water to go into the blood vessel, but don't know how to answer it in terms of hydrostatic pressure. 6. Imagine blood in a vessel that has an osmolarity of 300 mEq (a measure of the concentration of particles). Now, imagine that the IF around the...
I'm confused about how to use my calculator to find the critical value of t
I'm confused about how to use my calculator to find the critical value of t
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT