简体   繁体   中英

How to assert “N” exceptions have been thrown in this unit test?

I've got the following unit test, which works almost fine:

@Test(expectedExceptions = {IllegalArgumentException.class}, expectedExceptionsMessageRegExp =
        PasswordValidator.INVALID_PASSWORD)
public void testInvalidPasswordsThrowException() throws Exception {
    for (String invalidPassword: getInvalidPasswords()){
        new LaxPasswordValidator(invalidPassword);
    }
}

private String[] getInvalidPasswords() {
    return new String[] {INVALID_PASSWORD_1, INVALID_PASSWORD_2, INVALID_PASSWORD_3, INVALID_PASSWORD_4,
            INVALID_PASSWORD_5, INVALID_PASSWORD_6, INVALID_PASSWORD_7, INVALID_PASSWORD_8, INVALID_PASSWORD_9,
            INVALID_PASSWORD_10};
}

As you can see, it is just asserting that new LaxPasswordValidator(invalidPassword) works as expected and throws an exception.

Problem: It is just taking INVALID_PASSWORD_1 into account, so it stops in the first iteration; it launches the exception but does not continue checking the other passwords. How can I make it test them all? Thanks

You must annotate the method as @DataProvider

@DataProvider(name = "invalid-passwords")
public Object[][] getInvalidPasswords() {
    return new String[][]{
        {"INVALID_PASSWORD_1"},
        {"INVALID_PASSWORD_2"},
        {"INVALID_PASSWORD_3"}
    };
}

and annotate the @Test method to use this dataprovider for the parameters.

@Test(expectedExceptions = {IllegalArgumentException.class},
    expectedExceptionsMessageRegExp = PasswordValidator.INVALID_PASSWORD,
    dataProvider = "invalid-passwords")
public void testInvalidPasswords(String password) throws Exception {
    new LaxPasswordValidator(password);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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