首页 > 解决方案 > 测试用例错误 square(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);
    }
}

标签: javacomputer-science

解决方案


让我猜猜,应该有:

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

它应该是

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

推荐阅读