我想在我的蛇游戏的junit中制作测试用例。 我有一个GAMEOVER方法,我想测试一下:具有图形参数的方法的Junit测试用例
public void gameOver(Graphics g) {
String msg = "Game Over";
final Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg))/2,
HEIGHT/2);
}
我的主类是董事会和它扩展JPanel。测试:
public void testGameOver() {
System.out.println("gameOver");
Board instance = new Board();
Graphics g = instance.getGraphics();
instance.gameOver(g);
Color tmp = new Color(instance.getBackground().getRGB());
assertEquals(tmp,Color.white.getRGB());
assertEquals(instance.getFont().getFontName(),new Font("Helvetica", Font.BOLD, 14).getFontName());
}
当我尝试在实例上运行gameOver方法时,我得到一个java.lang.NullPointerException。请帮忙!!我是Junit的新手。
Testcase: testGameOver(snake.BoardTest): Caused an ERROR
null
java.lang.NullPointerException
at snake.Board.gameOver(Board.java:121)
at snake.BoardTest.testGameOver(BoardTest.java:67)
请发布包含java.lang.NullPointerException的错误堆栈跟踪。 – Scott