jump测试
测试前面有两个空位时
@Test
public void testJump() {
ActorWorld world = new ActorWorld();
Jumper jumper = new Jumper();
world.add(new Location(4, 4), jumper);
int dire = jumper.getDirection();
jumper.act();
int dire2 = jumper.getDirection();
assertEquals("The jumper jump over blank error", 2, jumper.getLocation().getRow());
assertEquals("The jumper jump over blank error", 4, jumper.getLocation().getCol());
assertTrue("The jumper doesn't turn error", dire == dire2);
}
测试前面一个是空的前面第二个是花时
@Test
public void testJumpFlowerAndRock() {
ActorWorld world = new ActorWorld();
Flower flower = new Flower();
Jumper jumper = new Jumper();
world.add(new Location(3, 4), flower);
world.add(new Location(4, 4), jumper);
int olddire = jumper.getDirection();
jumper.act();
int newdire = jumper.getDirection();
assertEquals("The jumper jump over flower error", 2, jumper.getLocation().getRow());
assertEquals("The jumper jump over flower error", 4, jumper.getLocation().getCol());
assertTrue("The jumper doesn't turn error", olddire == newdire);
Rock rock = new Rock();
Jumper jumper2 = new Jumper();
world.add(new Location(3, 2), rock);
world.add(new Location(4, 2), jumper2);
int predire2 = jumper2.getDirection();
jumper2.act();
int aftdire2 = jumper2.getDirection();
assertEquals("The jumper jump over rock error", 2, jumper2.getLocation().getRow());
assertEquals("The jumper jump over rock error", 2, jumper2.getLocation().getCol());
assertTrue("The jumper doesn't turn error", predire2 == aftdire2);
}
测试前面一个是花前面第二个是空时
@Test
public void testNoJumpFlowerAndRock() {
ActorWorld world = new ActorWorld();
Flower flower = new Flower();
Jumper jumper = new Jumper();
world.add(new Location(2, 4), flower);
world.add(new Location(4, 4), jumper);
int dire = jumper.getDirection();
jumper.act();
int dire2 = jumper.getDirection();
assertEquals("The jumper don't jump over flower error", 4, jumper.getLocation().getRow());
assertEquals("The jumper don't jump over flower error", 4, jumper.getLocation().getCol());
assertFalse("The jumper doesn't turn error", dire == dire2);
Rock rock = new Rock();
Jumper jumper2 = new Jumper();
world.add(new Location(2, 4), rock);
world.add(new Location(4, 4), jumper2);
int dd1 = jumper2.getDirection();
jumper2.act();
int dd2 = jumper2.getDirection();
assertEquals("The jumper don't jump over rock error", 4, jumper2.getLocation().getRow());
assertEquals("The jumper don't jump over rock error", 4, jumper2.getLocation().getCol());
assertFalse("The jumper doesn't turn error", dd1 == dd2);
}
测试如果调到前面两个critter会相遇时
@Test
public void testNoJumpActor() {
ActorWorld world = new ActorWorld();
Jumper jumper1 = new Jumper();
Jumper jumper2 = new Jumper();
world.add(new Location(3, 4), jumper1);
world.add(new Location(4, 4), jumper2);
jumper2.act();
jumper1.act();
assertEquals("The jumper don't jump over rock error", 1, jumper1.getLocation().getRow());
assertEquals("The jumper don't jump over rock error", 4, jumper1.getLocation().getCol());
assertEquals("The jumper don't jump over rock error", 4, jumper2.getLocation().getRow());
assertEquals("The jumper don't jump over rock error", 4, jumper2.getLocation().getCol());
}
测试前面一个会是边缘或者墙面时
@Test
public void testNoJumpEdge() {
ActorWorld world = new ActorWorld();
Jumper jumper = new Jumper();
world.add(new Location(1, 4), jumper);
int dire = jumper.getDirection();
jumper.act();
int dire2 = jumper.getDirection();
assertEquals("The jumper don't jump over flower error", 1, jumper.getLocation().getRow());
assertEquals("The jumper don't jump over flower error", 4, jumper.getLocation().getCol());
assertFalse("The jumper doesn't turn error", dire == dire2);
}
act测试
@Test
public void testAct() {
ActorWorld world = new ActorWorld();
Jumper jumper = new Jumper();
world.add(new Location(4, 4), jumper);
jumper.act();
assertEquals("The jumper act error", 4, jumper.getLocation().getRow());
fail("Not yet implemented");
}
turn测试
@Test
public void testTurn() {
Jumper jumper = new Jumper();
ActorWorld world = new ActorWorld();
world.add(new Location(4, 3), jumper);
jumper.turn();
assertEquals("The jumper turn error", 4, jumper.getLocation().getRow());
fail("Not yet implemented");
}
测试结果及详解
测试总结
这次测试我使用了eclipse里的junit5单元测试,之前是在命令行中测试的,测试结果与预期结果一致,因为程序比较简单,测试效率也比较高,通过这次测试也更加熟悉了junit的使用,但是要注意的是,junit测试只是默认的测试已有的函数,不会测试出来语义上理解错误造成的问题,所以还要多加注意语义理解错误的测试。