Get startedGet started for free

Username Validation Parameterized

As your Java knowledge increases, you've decided to revisit the username validator from Chapter 1. Since it depends on a single output, it can be shortened using parameterization.

Consider all the failure outcomes of the isValidUsername() method. Condense them all in a single @ParameterizedTest.

The necessary JUnit packages have been imported for you.

This exercise is part of the course

Introduction to Testing in Java

View Course

Exercise instructions

  • Use the correct annotation to mark a test as a parameterized test.
  • Add a null input to the test.
  • Add the annotation to the input values in the test.
  • Mark the value type of the inputs.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class Main {
    public static void main(String[] args) {
		launchTestsAndPrint(UsernameValidatorTest.class);
    }
}

class UsernameValidatorTest {
	
    // Annotate with the correct annotation for parameterized test
    @____ 
    // Add the annotation for inputting a null input
    @____ 
    // Add the annotation for inputting values and the type of the inputs
    @____(____ = {"john doe", "jd"}) 
    void isValidUsername_returnsFalse_whenSpaces(String username) {
        boolean actual = UsernameValidator.isValidUsername(username);

        assertFalse(actual);
    }
}
Edit and Run Code