繁体   English   中英

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

[英]JAVA: JUnitTest, testing method that throws two exceptions

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

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

主体具有两个try / catch块(对SO的搜索导致了一条帖子,提示您测试抛出异常的方式),每个异常一个。 在我看来,我应该将其分解为两种测试方法,尤其是因为我只能拥有一个预期的属性。 但是,当我这样做时,应该测试A8InvalidInputException的方法要求对A8AlreadyGuessedException进行尝试/捕获,而应该测试A8AlreadyGuessedException的方法就需要对A8InvalidInputException进行尝试/捕获。 我不太确定如何编写此测试。 这是我要测试的方法:

/**
 * 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);
    }
}

只需在throws子句中添加两个例外:

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

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

然后,如果一个测试引发另一个异常(意外的异常),则您的测试将自动失败。

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

您可能需要三个测试用例:一个在方法引发一个异常时,一个在方法引发另一个异常时,以及一个在方法完全不引发任何异常时进行。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM