代码之家  ›  专栏  ›  技术社区  ›  speedracer

JAVA:JUnitTest,抛出两个异常的测试方法

  •  0
  • speedracer  · 技术社区  · 13 年前

    我正在测试一个抛出两个不同异常的方法。这是我的标题:

    @Test (expected = A8InvalidInputException.class)
    public void testGuessCharacter() throws A8InvalidInputException, A8AlreadyGuessedException { ... }

    正文有两个try/catch块(在SO上搜索得到一个帖子,上面说这就是测试抛出异常的方法),每个异常一个。在我看来,我应该把它分成两种测试方法,尤其是因为我只能有一种 预期 属性然而,当我这样做时,应该测试A8InvalidInputException的方法需要对A8AlreadyGuessedException进行try/catch,而应该测试A8AlreadyGuessedException的方法则需要对A8InvalidInput Exception进行try-catch。我真的不知道该怎么写这个测试。这是我试图测试的方法:

    /**
     * This method returns whether a specified character exists in the keyPhrase field
     * @param guess  a character being checked for in the keyPhrase field
     * @return  returns whether a specified character exists in the keyPhrase field
     * @throws A8InvalidInputException  if a non-valid character is passed as input to this method
     * @throws A8AlreadyGuessedException  if a valid character which has already been guessed is passed as input to this method
     */
    public boolean guessCharacter(char guess) throws A8InvalidInputException, A8AlreadyGuessedException
    {
        if(isValidCharacter(guess))
        {
            guess = Character.toLowerCase(guess);
    
            if(guessedCharacters.contains(guess) )
            {
                throw new A8AlreadyGuessedException("" + guess);
            }
            else
            {
                guessedCharacters.add(guess);
                if(keyPhrase.contains("" + guess))
                    return true;
                else
                {
                    numberOfGuessesLeft--;
                    return false;
                }
            }       
        }
        else
        {
            throw new A8InvalidInputException("" + guess);
        }
    }
    
    4 回复  |  直到 13 年前
        1
  •  6
  •   Chris Knight    13 年前

    只需在throws子句中添加两个异常:

    @Test (expected = A8InvalidCharacterException.class) 
    public void testInvalidCharacterScenario() throws A8InvalidInputException, A8AlreadyGuessedException { ... }
    
    @Test (expected = A8InvalidInputException.class) 
    public void testInvalidInputScenario() throws A8InvalidInputException, A8AlreadyGuessedException { ... }
    

    然后,如果一个测试抛出另一个异常(意外的异常),那么您的测试将自动失败。

        2
  •  4
  •   Étienne Miret    13 年前

    一个方法在运行时只能引发一个异常。这就是为什么只能有一个预期的归因。

    您可能希望有三个测试用例:一个是当方法抛出一个异常时,一个是该方法抛出另一个异常,另一个是方法根本不抛出任何异常时。

    不要在您的 @Test 方法,只需声明它们抛出异常即可。

        3
  •  2
  •   user5398447 user5398447    13 年前

    是的,你应该把它分成两个单元测试。一个具有无效输入以触发 A8InvalidInputException 以及另一个具有“已猜到”输入以触发 A8AlreadyGuessedException .

        4
  •  1
  •   Makoto    13 年前

    考虑一下,为了使编写测试更简单,将它们分解为两个不同的部分——测试 isValidCharacter 和测试 guessCharacter 分别地

    推测 isValidCharacter(guess) 如果你得到一个无效的猜测,就会失败,我认为投掷 A8InvalidInputException 采用这种方法将是理想的。

    public boolean isValidCharacter(char guess) throws A8InvalidInputException {
        // have your logic to check the guess, and if it's invalid, throw
    }
    

    那么你所需要做的就是测试 那个 特定的方法,以查看它是否在伪输入上抛出异常。

    @Test (expected = A8InvalidInputException.class)
    public void testIsValidCharacterWithInvalidCharacter() {
        // write your test here.
    }
    

    接下来,你可以改变你的方法,只关心 isValid字符 方法,因为如果不返回布尔值,则抛出了异常。

    最后,您只关心 留言字符 无论是否投掷 A8AlreadyGuessedException .

    @Test (expected = A8AlreadyGuessedException.class)
    public void testGuessCharacterWithAlreadyGuessedValue() {
        // write your test here.
    }