有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java测试用例误差平方(int)在fox中具有私有访问权限

每次编译时,我都会遇到一个错误,即square(int)在fox中具有私有访问权限 并突出显示此代码“assertEquals(25,tod.square(5),.001);”我不知道为什么它一直这么说,这些代码在我看来很好。这应该是一个代码的测试类,即使它不是必需的。 下面是完整的代码

public class FoxTest extends TestCase
{
    //~ Fields ................................................................


    //~ Constructor ...........................................................

    // ----------------------------------------------------------
    /**
     * Creates a new FoxTest test object.
     */
    public FoxTest()
    {
        // The constructor is usually empty in unit tests, since it runs
        // once for the whole class, not once for each test method.
        // Per-test initialization should be placed in setUp() instead.
    }


    //~ Methods ...............................................................

    // ----------------------------------------------------------
    /**
     * Sets up the test fixture.
     * Called before every test case method.
     */
    public void setUp()
    {
        /*# Insert your own setup code here */
    }


    // ----------------------------------------------------------
    /**
       * test the constructor
       */
      public void testConst()
      {
          Fox tod = new Fox();
          assertEquals(3, tod.getSpeed());
      }
    /**
      * test the distance to
      */
     public void testDistance()
     {
         Fox tod = new Fox();
         tod.setGridX(0);
         tod.setGridY(0);
         Fox ring = new Fox();
         ring.setGridX(0);
         ring.setGridY(3);
         assertEquals(3, ring.distanceTo(tod), .001);
     }
     
    /**
      * test the nearestRabbit
      */
     public void testNear()
     {
         Field field = new Field(400, 400, 0, 0);
         Fox tod = new Fox();
         field.add(tod, 0, 0);
         Rabbit reng = new Rabbit();
         field.add(reng, 0, 5);
         Rabbit ring = new Rabbit();
         field.add(ring, 0, 3);
         Rabbit rong = new Rabbit();
         field.add(rong, 0, 6);
         assertEquals(ring, tod.nearestRabbit());
        }
    /**
      * test the nearestRabbit
      */
     public void testNearNull()
      {
          Field field = new Field(400, 400, 0, 0);
          Fox tod = new Fox();
          field.add(tod, 0, 0);
          assertEquals(null, tod.nearestRabbit());
            }
    /**
      * test the turn()
      */
     public void testTurn()
      {
          Field field = new Field(400, 400, 0, 0);
          Fox tod = new Fox();
          field.add(tod, 0, 0);
          Rabbit reng = new Rabbit();
          field.add(reng, 0, 5);
          Rabbit ring = new Rabbit();
          field.add(ring, 1, 1);
          Rabbit rong = new Rabbit();
          field.add(rong, 0, 6);
          tod.turn();
          assertEquals(45, tod.getRotation(), .001);
      }
    /**
      * test square
      */
     public void testSquare()
      {
          Fox tod = new Fox();
          assertEquals(25, tod.square(5), .001);
      }
    /**
      * test the act() method
      */
     public void testAct()
      {
          Field field = new Field(400, 400, 0, 0);
          Fox tod = new Fox();
          field.add(tod, 0, 0);
          Rabbit ring = new Rabbit();
          field.add(ring, 7, 7);
          tod.act();
          assertEquals(45, tod.getRotation(), .001);
      }
    /**
     * test the act() method
     */
    public void testActDone()
    {
        Field field = new Field(400, 400, 0, 0);
        Fox tod = new Fox();
        field.add(tod, 0, 0);
        Rabbit reng = new Rabbit();
        field.add(reng, 0, 5);
        Rabbit ring = new Rabbit();
        field.add(ring, 1, 1);
        Rabbit rong = new Rabbit();
        field.add(rong, 0, 6);
        tod.act();
        List<Rabbit> rabbits = field.getObjects(Rabbit.class);
        assertEquals(2, rabbits.size());
    }
    /**
     */
    public void testTurnNull()
    {
        Field field = new Field(400, 400, 0, 0);
        Fox tod = new Fox();
        field.add(tod, 0, 0);
        assertEquals(null, tod.nearestRabbit());
        tod.turn();
        assertEquals(0, tod.getRotation(), .001);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    让我猜猜,应该有:

    public class Fox {
      private int square(int x) {
        return x * x;
      }
    }
    

    应该是这样

    public class Fox {
      public int square(int x) {
        return x * x;
      }
    }