Java 测试Junit随机类

Java 测试Junit随机类,java,testing,random,junit,Java,Testing,Random,Junit,我有一个Game.java类 package jmine; import java.util.Random; import jmine.exceptions.IllegalNumOfMine; public class Game { private Box[][] ground; private int width; private int height; private int numOfMine; private boolean game

我有一个Game.java类

    package jmine;

import java.util.Random;
import jmine.exceptions.IllegalNumOfMine;

public class Game {

    private Box[][] ground;
    private int width;
    private int height;
    private int numOfMine;
    private boolean gameOver;

    public Game(int pWidth, int pHeight, int pNumOfMine) throws IllegalNumOfMine {
        if (pNumOfMine > (pWidth * pHeight)) {
            throw new IllegalNumOfMine();
        }
        this.ground = new Box[pHeight][pWidth];
        this.width = pWidth;
        this.height = pHeight;
        this.numOfMine = pNumOfMine;
        this.gameOver = false;
        this.newGame();
    }

    public void newGame() {
        this.gameOver=false;
        Random randomizer = new Random(); 
        for (int i = 0; i < this.width; i++) {
            for (int j = 0; j < this.height; j++) {
                this.ground[j][i] = new Box();
            }
        }
        int randI = 0;
        int randJ = 0;

        for(int i=0;i<this.numOfMine;i++){
            if (i<this.width){
            randI=i;
            randJ=(this.width-1)-i;}
            else{
                randI=0;
                randJ=0;
            }

        /*for (int i = 0; i < this.numOfMine; i++) {
            do {
                randI = randomizer.nextInt(this.height);
                randJ = randomizer.nextInt(this.width);
            } while (this.ground[randI][randJ].isMine());*/
            this.ground[randI][randJ].setMine(true);
            if (randI - 1 >= 0 && randJ - 1 >= 0) {
                this.ground[randI - 1][randJ - 1].upNumOfMine();
            }
            if (randI - 1 >= 0) {
                this.ground[randI - 1][randJ].upNumOfMine();
            }
            if (randI - 1 >= 0 && randJ + 1 < this.width) {
                this.ground[randI - 1][randJ + 1].upNumOfMine();
            }
            if (randJ - 1 > 0) {
                this.ground[randI][randJ - 1].upNumOfMine();
            }
            if (randJ + 1 < this.width) {
                this.ground[randI][randJ + 1].upNumOfMine();
            }
            if (randI + 1 < this.height && randJ - 1 >= 0) {
                this.ground[randI + 1][randJ - 1].upNumOfMine();
            }
            if (randI + 1 < this.height) {
                this.ground[randI + 1][randJ].upNumOfMine();
            }
            if (randI + 1 < this.height && randJ + 1 < this.width) {
                this.ground[randI + 1][randJ + 1].upNumOfMine();
            }
        }
    }

    public Box[][] getGround() {
        return this.ground;
    }

    public int getHeight() {
        return this.height;
    }

    public int getNumOfMine() {
        return this.numOfMine;
    }

    public int getWidth() {
        return this.width;
    }

    public boolean getGameOver() {
        return this.gameOver;
    }

    public void flag(int pX, int pY) {
        if (this.ground[pX][pY].isCover()) {
            if (this.ground[pX][pY].isFlagged()) {
                this.ground[pX][pY].setFlagged(true);
            } else {
                this.ground[pX][pY].setFlagged(true);
            }
        }
    }

    public boolean isWinner(){
        for(int i=0; i<this.width; i++){
            for(int j=0; j<this.height; j++){
                if(this.ground[i][j].isCover()){
                    if(!this.ground[i][j].isMine()){
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public void unconver(int pX, int pY) {
        if (this.ground[pX][pY].isCover() && !this.ground[pX][pY].isFlagged()) {
            if (this.ground[pX][pY].isMine()) {
                this.gameOver = true;
            } else {
                this.ground[pX][pY].setCover(false);
                if (this.ground[pX][pY].getNumOfMine() == 0) {
                    if (pX - 1 >= 0 && pY - 1 >= 0) {
                        this.unconver(pX - 1, pY - 1);
                    }
                    if (pX - 1 >= 0) {
                        this.unconver(pY-1, pX);
                    }
                    if (pX - 1 >= 0 && pY + 1 < this.width) {
                        this.unconver(pX - 1, pY + 1);
                    }
                    if (pY - 1 >= 0) {
                        this.unconver(pX, pY - 1);
                    }
                    if (pY + 1 < this.width) {
                        this.unconver(pX, pY + 1);
                    }
                    if (pX + 1 < this.height && pY - 1 >= 0) {
                        this.unconver(pX + 1, pY - 1);
                    }
                    if (pX + 1 < this.height) {
                        this.unconver(pX + 1, pY);
                    }
                    if (pX + 1 < this.height && pY + 1 < this.width) {
                        this.unconver(pX + 1, pY + 1);
                    }
                }
            }
        }
    }
}
问题是我不知道如何强制randJ=1的mine值,以测试randJ-1=0时box[Randi][randJ-1]会发生什么


我能做的唯一方法是修改原始代码enter insert​​手动RandI和RandJ,但我认为这不是正确的方法。

您可以向Game类中注入随机发生器对象。然后,您可以模拟随机发生器并测试游戏类

if (randJ - 1> 0) {
                 this.ground [Rand i] [randJ - 1]. upNumOfMine ();
             }