From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Parameterized tests with ValueSource in JUnit 5

Parameterized tests with ValueSource in JUnit 5

- [Instructor] Let's refactor a non-enum test class to use parameterized tests. Here, we have a class with several tests that verify the functionality of an isPalindrome method. We can simplify this using the parameterized test and value source annotations. We'll separate those that are palindromes and those that are not palindromes into separate tests. Then we'll use the ValueSource annotation to test specific cases. For is palindrome, we'll pass in the empty string, a racecar, RaceCar with capitalization, and then those that are not palindromes would be hello, world, java, and JUnit. Let's delete these other tests and run it. And the tests pass. In our test results, we can see each test run, and run a specific case again, if we want to. For example, we could run just a again with the play button, and only that case will be run. With parametrized tests, we can consolidate similar test cases into single parametrized test methods.

Contents