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

时间:2013-03-09 22:36:29

标签: java exception junit junit4

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

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

正文有两个try / catch块(在SO上搜索会产生一个帖子,说明你是如何测试抛出异常的),每个例外都有一个。在我看来,我应该将其分解为两种测试方法,特别是因为我只能有一个期望的属性。但是,当我这样做时,应该测试A8InvalidInputException的方法需要针对A8AlreadyGuessedException的try / catch,并且应该测试A8AlreadyGuessedException的方法需要针对A8InvalidInputException的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 个答案:

答案 0 :(得分:6)

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

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

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

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

答案 1 :(得分:4)

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

您可能希望有三个测试用例:一个是方法抛出一个异常,一个是方法抛出另一个异常,另一个是方法没有抛出任何异常。

不要在@Test方法中放置任何try / catch语句,只是声明它们会抛出异常。

答案 2 :(得分:2)

是的,您应该将其分解为两个单元测试。一个用无效输入触发A8InvalidInputException,另一个用“已经猜到”输入触发A8AlreadyGuessedException

答案 3 :(得分:1)

考虑一下,让你的测试编写得更简单一些,将它们分成两个不同的部分 - 分别测试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.
}

接下来,您可以将方法更改为仅关注isValidCharacter方法的happy-path,因为如果不返回布尔值,则抛出异常。

最后,您只关注guessCharacter的测试是否会引发A8AlreadyGuessedException

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