java - 测试用例错误 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);
}
}
解决方案
让我猜猜,应该有:
public class Fox {
private int square(int x) {
return x * x;
}
}
它应该是
public class Fox {
public int square(int x) {
return x * x;
}
}
推荐阅读
- php - PHP 电子邮件清理过滤器
- node.js - Docker 中的 NodeJS 看不到连接
- c# - 是否有任何属性可以选择 ListView 项目设计时间
- css - 如何在css中用一种颜色绘制半对角线,用另一种颜色绘制另一半对角线
- r - 如何保持数据文件中相关图标签的顺序相同?
- lua - Lua中的快速序列化/反序列化表(无递归)
- d3.js - 存储多个 (~10000) SVG 填充模式
- jsp - 在第 39 行处理 JSP 页面 /index.jsp 时发生异常
- swift - 在 Metal 着色器中写入 0-Alpha 无法正确渲染
- java - 无法在 Raspberry Pi 3(Raspbian)上运行我的 JavaFX jar 文件