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

fjaof16o  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(317)

每次编译时,我都会遇到一个错误,指出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);
    }
}
dnph8jn4

dnph8jn41#

让我猜猜,应该有:

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

应该是的

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

相关问题