文章目录
Part3:测试文档
根据设计文档中提出的多种情况,再加上定义时提到的基本的跳跃共7种情况,我们使用Junit编写了测试,具体测试内容如下:
注:
- 所有代码存于JumperTest.java文件中,但由于显示所有代码的话显得太长,我们下面只显示ActorWorld添加Actor的操作以及断言部分的代码
- FIRST_COL代表2;SECOND_COL代表4;THIRD_COL代表6
1、测例展示
pre.基本跳跃:
我们利用Jumper的定义实现了两组测例,分别是花和石头摆在Jumper的前方相邻格,则如果没有错误的话Jumper会直接跳过去到达下一格,而花和石头保留在原地
- 具体测试代码:
// @Method: public void testCase0() // Rock Test world.add(new Location(3, FIRST_COL), jumper1); world.add(new Location(2, FIRST_COL), rock); jumper1.act(); assertEquals(new Location(1, FIRST_COL), jumper1.getLocation()); assertEquals(Location.NORTH, jumper1.getDirection()); assertNotNull("Rock is missing", rock.getLocation()); // Flower Test world.add(new Location(3, SECOND_COL), jumper2); world.add(new Location(2, SECOND_COL), flower); jumper2.act(); assertEquals(new Location(1, SECOND_COL), jumper2.getLocation()); assertEquals(Location.NORTH, jumper2.getDirection()); assertNotNull("Flower is missing", flower.getLocation());
a. Jumper目标格是Flower/Rock
在设计文档中我们已经说明目标格有花的话我们直接跳过去覆盖掉花;如果第2格是石头而第1格为空的话我们执行Move操作。因此我们根据相邻格是否可达设计两个测例。
- 具体测试代码
// @Method: public void testCaseA() // Flower test world.add(new Location(4, FIRST_COL), jumper1); world.add(new Location(2, FIRST_COL), flower); jumper1.act(); assertEquals(new Location(2, FIRST_COL), jumper1.getLocation()); assertEquals(Location.NORTH, jumper1.getDirection()); assertNull("Flower should be missing", flower.getLocation()); // Rock test world.add(new Location(4, SECOND_COL), jumper2); world.add(new Location(2, SECOND_COL), rock); jumper2.act(); assertEquals(new Location(3, SECOND_COL), jumper2.getLocation()); assertEquals(Location.NORTH, jumper2.getDirection()); assertNotNull("Rock is missing", rock.getLocation());
b. Jumper目标格超出边界
在设计文档我们已经说明此时Jumper无法移动两格,会根据相邻格是否可达决定Move还是Turn,因此我们据此设计两个测例。
- 具体测试代码:
// @Method: public void testCaseB() // 相邻格为可达位置 world.add(new Location(1, FIRST_COL), jumper1); jumper1.act(); assertEquals(new Location(0, FIRST_COL), jumper1.getLocation()); assertEquals(Location.NORTH, jumper1.getDirection()); // 相邻格为石头等不可达位置 world.add(new Location(1, SECOND_COL), jumper2); world.add(new Location(0, SECOND_COL), rock); jumper2.act(); assertEquals(new Location(1, SECOND_COL), jumper2.getLocation()); assertEquals(Location.NORTHEAST, jumper2.getDirection()); assertNotNull("Rock is missing", rock.getLocation());
c. Jumper正面对边界
这里测例设计非常简单,只需要一个:直接将Jumper放在边界处面对边界即可,最终Jumper会选择Turn。
- 具体测试代码:
// @Method: public void testCaseC() world.add(new Location(0, FIRST_COL), jumper); jumper.act(); assertEquals(new Location(0, FIRST_COL), jumper.getLocation()); assertEquals(Location.NORTHEAST, jumper.getDirection());
d. Jumper目标格是Actor(不是花/石头)
照设计文档,这里需要有三种测例,前两种目标格都是不移动的普通Actor,区别在于相邻格是否可达(可达->Move/不可达->Turn);剩下一种目标格是可移动的Actor,这样在前方Actor移动后,本Jumper也会Jump。
- 具体测试代码:
// @Method: public void testCaseD() // 相邻格是可达位置,目标格是普通Actor world.add(new Location(4, FIRST_COL), jumper1); world.add(new Location(2, FIRST_COL), actor1); jumper1.act(); assertEquals(new Location(3, FIRST_COL), jumper1.getLocation()); assertEquals(Location.NORTH, jumper1.getDirection()); // 相邻格是不可达位置,目标格是普通Actor world.add(new Location(4, SECOND_COL), jumper2); world.add(new Location(3, SECOND_COL), rock); world.add(new Location(2, SECOND_COL), actor2); jumper2.act(); assertEquals(new Location(4, SECOND_COL), jumper2.getLocation()); assertEquals(Location.NORTHEAST, jumper2.getDirection()); // 目标格是可移动的Actor world.add(new Location(4, THIRD_COL), jumper3); world.add(new Location(2, THIRD_COL), bug); bug.act(); jumper3.act(); assertEquals(new Location(1, THIRD_COL), bug.getLocation()); assertEquals(Location.NORTH, bug.getDirection()); assertEquals(new Location(2, THIRD_COL), jumper3.getLocation()); assertEquals(Location.NORTH, jumper3.getDirection());
e. Jumper移动路径中遇上别的Jumper
这里同样有三种可能,因此设计3个测例,每个测例中有两个Jumper:
- 第1个测例中双方目标格一样,且是不可达位置,那么将两个Jumper面对面摆放,中间隔3格,一方面中心格放石头让目标格变成不可达;另一方面其中一边相邻格加上石头,从而两个Jumper形成对照。
最终预期结果:相邻格有石头的Jumper选择Turn,另一个选择Move- 第2个测例中双方目标格一样,且是可达位置,那么将两个Jumper面对面摆放,中间隔3格。
最终预期结果:先调用Act的Jumper选择Jump,另一个选择Move- 第3个测例中双方目标格不一样,我们这里测试同样将将两个Jumper面对面摆放,不过中间隔2格,让双方目标格不一样。
最终预期结果:两个Jumper都选择Jump
- 具体测试代码:
// @Method: public void testCaseE() // 双方目标格一样且是不可达位置 world.add(new Location(5, FIRST_COL), jumper1); world.add(new Location(1, FIRST_COL), jumper2); jumper2.setDirection(Location.SOUTH); world.add(new Location(4, FIRST_COL), rock1); world.add(new Location(3, FIRST_COL), rock2); jumper1.act(); jumper2.act(); assertEquals(new Location(5, FIRST_COL), jumper1.getLocation()); assertEquals(Location.NORTHEAST, jumper1.getDirection()); assertEquals(new Location(2, FIRST_COL), jumper2.getLocation()); assertEquals(Location.SOUTH, jumper2.getDirection()); // 双方目标格一样且是可达位置 world.add(new Location(5, SECOND_COL), jumper3); world.add(new Location(1, SECOND_COL), jumper4); jumper4.setDirection(Location.SOUTH); jumper4.act(); jumper3.act(); assertEquals(new Location(4, SECOND_COL), jumper3.getLocation()); assertEquals(Location.NORTH, jumper3.getDirection()); assertEquals(new Location(3, SECOND_COL), jumper4.getLocation()); assertEquals(Location.SOUTH, jumper4.getDirection()); // 双方目标格不一样 world.add(new Location(5, THIRD_COL), jumper5); world.add(new Location(2, THIRD_COL), jumper6); jumper6.setDirection(Location.SOUTH); jumper5.act(); jumper6.act(); assertEquals(new Location(3, THIRD_COL), jumper5.getLocation()); assertEquals(Location.NORTH, jumper5.getDirection()); assertEquals(new Location(4, THIRD_COL), jumper6.getLocation()); assertEquals(Location.SOUTH, jumper6.getDirection());
f. 其他测试——Jumper前方连续两个石头
这里测试跟标题一样,直接在Jumper正前方设置两个石头即可,最终照设计文档Jumper会选择Turn。
- 具体测试代码:
// @Method: public void testCaseF() world.add(new Location(4, FIRST_COL), jumper); world.add(new Location(3, FIRST_COL), rock1); world.add(new Location(2, FIRST_COL), rock2); jumper.act(); assertEquals(new Location(4, FIRST_COL), jumper.getLocation()); assertEquals(Location.NORTHEAST, jumper.getDirection());
2、最终结果
(1) Junit单元测试
测试环境
- Windows: Java15 / Junit4.13
- Ubuntu: Java1.8 / Junit4.13
最终在Windows和Ubuntu环境下进行了测试,最终都通过了,以Windows为例Junit测试结果如下图:
(2) SonarQube代码检查
而整个Jumper用Sonar-qube分析的结果如下,可以看到满足要求: