阶段二 Part3 测试报告
针对 阶段二 Part3 设计报告 中的 5 种可能出现的情况设计 JUnit 测试。
a. 当 Jumper 前 2 格位置存在一个 Flower 对象或 Rock 对象,而前 1 格为空时:Jumper 只向前移动一格。
设计 testA
如下,Jumper 对象 alice 位于 (5,5) 且前进方向为 Location.NORTH,其前 2 格位置 (3,5) 存在一个 Flower 对象。
测试 alice 的下一移动是否符合设计,即只移动一格到达位置 (4,5)。
@Test
public void testA() {
ActorWorld world = new ActorWorld();
Jumper alice = new Jumper();
Flower f = new Flower();
world.add(new Location(5, 5), alice);
world.add(new Location(3,5), f);
alice.act();
assertEquals(true, alice.getLocation().equals(new Location(4,5)));
}
b. 当 Jumper 前 2 格位置在当前 grid 之外时:检查 Jumper 前 1 格是否在 grid 中、是否为空,若是,jumper 只向前移动 1 格。
设计 testB
如下,Jumper 对象 alice 位于 (1,3) 且前进方向为 Location.NORTH,其前 2 格位置 (-1,3) 在当前 grid 之外。
测试 alice 的下一移动是否符合设计,即只移动一格到达位置 (0,3)。
@Test
public void testB() {
ActorWorld world = new ActorWorld();
Jumper alice = new Jumper();
world.add(new Location(1, 3), alice);
alice.act();
assertEquals(true, alice.getLocation().equals(new Location(0,3)));
}
c. 当 Jumper 前 1 格位置在当前 grid 之外时: Jumper 顺时针旋转 45 度。
设计 testC
如下,Jumper 对象 alice 位于 (0,3) 且前进方向为 Location.NORTH,其前 1 格位置 (-1,3) 在当前 grid 之外。
测试 alice 的下一移动是否符合设计,即前进方向变为 45 且位置不变仍然位于 (0,3)。
@Test
public void testC() {
ActorWorld world = new ActorWorld();
Jumper alice = new Jumper();
world.add(new Location(0, 3), alice);
alice.act();
assertEquals(45, alice.getDirection());
assertEquals(true, alice.getLocation().equals(new Location(0,3)));
}
d. 当 Jumper 前 2 格位置存在一个非 Flower 对象或 Rock 对象的 Actor 对象时:若 jumper 前 1 格为空,与 a 相同,jumper 只向前移动 1 格;若 jumper 前 1 格被占领,与 c 相似,jumper 转向。
设计 testD
如下,Jumper 对象 alice 位于 (5,5) 且前进方向为 Location.NORTH,第二个 Jumper 对象 bob 位于 alice 的前 2 格位置 (3,5) 。
测试 alice 的下一移动是否符合设计,即只移动一格到达位置 (4,5),同时测试 bob 是否正常移动,即移动两格到达位置 (1,5)。
@Test
public void testD() {
ActorWorld world = new ActorWorld();
Jumper alice = new Jumper();
Jumper bob = new Jumper();
world.add(new Location(5, 5), alice);
world.add(new Location(3,5), bob);
alice.act();
bob.act();
assertEquals(true, alice.getLocation().equals(new Location(4,5)));
assertEquals(true, bob.getLocation().equals(new Location(1,5)));
}
e. 当 Jumper 前 1 格位置存在另一个非 Jumper 对象时: Jumper 顺时针旋转 45 度。
设计 testE
如下,Jumper 对象 alice 位于 (3,5) 且前进方向为 Location.NORTH,第二个 Jumper 对象 bob 位于 alice 的前 1 格位置 (2,5) 且前进方向为 Location.SOUTH。
测试 alice 的下一移动是否符合设计,即前进方向变为 45 且位置不变仍然位于 (3,5),同时测试 bob 的下一移动是否符合设计,即前进方向变为 Location.SOUTHWEST 且位置不变仍然位于 (2,5)。
@Test
public void testE() {
ActorWorld world = new ActorWorld();
Jumper alice = new Jumper();
Jumper bob = new Jumper();
world.add(new Location(3, 5), alice);
world.add(new Location(2,5), bob);
bob.setDirection(Location.SOUTH);
alice.act();
bob.act();
assertEquals(45, alice.getDirection());
assertEquals(true, alice.getLocation().equals(new Location(3,5)));
assertEquals(Location.SOUTHWEST, bob.getDirection());
assertEquals(true, bob.getLocation().equals(new Location(2,5)));
}
运行 JUnit 测试结果如下,可见全部测试通过。