java—当异常处理不当时,为什么要接受测试

1u4esq0p  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(114)

考虑这个班

package at.jku.ssw.queens;

public class Queens {

public static Queens create(int n) throws IllegalArgumentException {
    if (n < 0) {
        throw new IllegalArgumentException();
    }
    return new Queens(n);
}
// TODO: fields
private int board[][];
private int nOfQueens;

public Queens(int n) {
    // TODO
    this.board = new int [n][n];
    this.nOfQueens = nOfQueens;
}

public int getSize() {
    // TODO
    return board.length;
}

public int getNQueens() {
    // TODO
    return nOfQueens;
}
//Hilfsmethoden
public boolean checkCol(char col) {
    return col == 'A' || col == 'B' || col == 'C' || col == 'D' || col == 'E' || col == 'F' || col == 'G' || col == 'H';
}
//Hilfsmethoden
public boolean checkRow(int row) {
    return row >= 1 && row <= board.length;
}

public State setQueen(int row, char col) throws QueensException {
    // TODO
    if (!checkRow(row) || !checkCol(col)) {
        throw new InvalidPositionException();
    } else if (hasQueen(row, col)) {
        throw new InvalidMoveException();
    } else if (checkRow(row) && checkCol(col)) {
        board[row+1][col-65] = 'Q';
        nOfQueens++;
    }
    return getGameState();
}

public State removeQueen(int row, char col) throws QueensException {
    // TODO
    if (!checkRow(row) || !checkCol(col)) {
        throw new InvalidPositionException();
    } else if (!hasQueen(row, col)) {
        throw new InvalidMoveException();
    } else if (hasQueen(row, col)) {
        board[row+1][col-65] = 0;
        nOfQueens--;
    }
    return getGameState();
}

public boolean hasQueen(int row, char col) throws InvalidPositionException {
    // TODO
    if (!checkRow(row) || !checkCol(col)) {
        throw new InvalidPositionException();
    } else
        return board[row+1][col-65] == 'Q';
}

public State getGameState() {
    int numberOfQueens = getNQueens();

    if (numberOfQueens == getSize()) {
        return State.COMPLETE;
    } else if (numberOfQueens < getSize()) {
        return State.INCOMPLETE;
    }
    return State.INVALID;
}

public void clear() {
    // TODO
    removeAll();
    nOfQueens = 0;
}
public void removeAll() {
    for(int row = 0; row < board.length; row++) {
        for(int col = 0; col < board.length; col++) {
            board[row][col] = 0;
        }
    }
}

}
这个junit测试类:

package at.jku.ssw.queens.test;

import static org.junit.jupiter.api.Assertions.*;

import at.jku.ssw.queens.Queens;
import org.junit.jupiter.api.Test;

class QueensTest {

    @Test
    void testCreate() { //passt
        assertNotNull(Queens.create(5));
        assertThrows(IllegalArgumentException.class, () -> Queens.create(-1));
    }

    @Test
    void testGetSize() { // passt
        Queens queen = Queens.create(5);
        assertEquals(queen.getSize(), 5);
    }

    @Test
    void testgetNQueens() { // index 5 out of bound for lenght 5
        Queens queen = Queens.create(5);
        try {
            queen.setQueen(2, 'B');
            assertTrue(queen.hasQueen(2, 'B'));
            queen.setQueen(3, 'C');
            assertTrue(queen.hasQueen(3, 'C'));
            queen.setQueen(1, 'E');
            assertTrue(queen.hasQueen(1, 'E'));
            queen.setQueen(4, 'F');
            assertTrue(queen.hasQueen(4, 'F'));
            queen.setQueen(5, 'H');
            assertTrue(queen.hasQueen(5, 'H'));

        } catch (QueensException e) {
            e.printStackTrace();
        }

        assertEquals(5, queen.getNQueens());

    }

    @Test
    void testSetQueen() { // Invalid exception
        Queens queen = Queens.create(5);
        assertThrows(InvalidPositionException.class, () -> queen.setQueen(-1, 'H'));
        try {
            queen.setQueen(1, 'A');
            assertTrue(queen.hasQueen(1, 'A'));
            queen.setQueen(8, 'H');
            assertTrue(queen.hasQueen(8, 'H'));
        } catch (QueensException e) {
            e.printStackTrace();
        }
    }
    @Test
    void testRemoveQueen() { //passt
         final Queens queen = Queens.create(5);
        try {
            queen.setQueen(1,'A');
        } catch (QueensException e) {
            e.printStackTrace();
        }

        try {
            queen.removeQueen(1,'A');
        } catch (QueensException e) {
            e.printStackTrace();
            fail();
        }
    }
    @Test
    void testhasQueen() { //passt
        Queens queen = Queens.create(5);
        try {
            assertFalse(queen.hasQueen(2, 'B'));
            try {
                queen.setQueen(3,'A');
            } catch (QueensException e) {
                e.printStackTrace();
            }
            assertTrue((queen.hasQueen(3, 'A')));
        }
        catch (QueensException e) {
            e.printStackTrace();
            fail();
        }
        assertThrows(InvalidPositionException.class,() -> queen.hasQueen(2,'Y'));
    }

    @Test
    void testGameState() { // index out of bound
        Queens queen = Queens.create(5);
        try {
            queen.setQueen(1,'A');
        } catch (QueensException e) {
            e.printStackTrace();
        }
        try {
            queen.setQueen(3,'C');
        } catch (QueensException e) {
            e.printStackTrace();
        }
        try {
            queen.setQueen(2,'F');
        } catch (QueensException e) {
            e.printStackTrace();
        }
        assertEquals(State.INCOMPLETE,queen.getGameState());
        try {
            queen.setQueen(2, 'F');
        } catch (QueensException e) {
            e.printStackTrace();
        }
        assertEquals(State.INVALID,queen.getGameState());
    }

    @Test
    void testClear() { //invalid exception
        Queens queen = Queens.create(5);
        try {
            queen.setQueen(1,'B');
        } catch (QueensException e) {
            e.printStackTrace();
        }
        try {
            queen.removeQueen(3,'A');
        } catch (QueensException e) {
            e.printStackTrace();
        }
        queen.clear();
        assertEquals(0,queen.getNQueens());
    }
}

现在我面临一个让我困惑的问题。intellij接受testsetqueen和testclear方法为成功,这意味着测试通过。但是在控制台上,我得到了testclear方法的这个错误
at.jku.ssw.queens.test.invalidmoveexception at.jku.ssw.queens.queens.removequeen(queens.java:61)
这是testsetqueen方法的一部分
at.jku.ssw.queens.test.invalidpositionexception at.jku.ssw.queens.queens.setqueen(queens.java:46)
方法getnqueens和getgamestate正在生成arrayindexoutofbound异常,其他方法工作正常。我的问题是,当我在(setqueen)中得到一个错误的方法被用在其他测试方法中时,为什么有些方法可以很好地工作,而其他方法却不能。是什么导致了异常错误,因为在其他方法中它可以正常工作?当一个位置无效时,应该抛出异常,因此当checkrow和checkcol为false时,问题是否在于我实现这两个方法的方式?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题