JUnit 5参数化测试配置错误及FizzBuzz测试失败求助
Hey there! Let's break down why your parameterized test is throwing that error and how to fix it.
The Root Cause
Looking at your multiplesOf5Inputs method, the problem lies in how you're using the skip() operations:
IntStream.range(1,60)generates numbers from 1 to 59 (since it's exclusive of the upper bound).skip(15)removes the first 15 numbers (1-15), leaving 16-59.skip(30)then removes the next 30 numbers (16-45), leaving only 14 numbers (46-59).- Calling
skip(45)on a stream with just 14 elements skips all of them, resulting in an empty stream. The finalskip(60)doesn't affect anything at this point—you've already lost all potential test inputs.
The Fix
Instead of using skip() (which is meant to skip a fixed number of elements, not filter specific values), you should use filter() to target exactly the numbers that should return "Buzz" in FizzBuzz: multiples of 5 that are not multiples of 3 (since multiples of both would return "FizzBuzz").
Here's the corrected method:
private static Stream<Integer> multiplesOf5Inputs(){ return IntStream.range(1,60) .filter(i -> i % 5 == 0 && i % 3 != 0) // Keep valid "Buzz" candidates .mapToObj(i -> i); }
Why This Works
This filter will select numbers like 5, 10, 20, 25, etc.—all values where your FizzBuzz method should return "Buzz". Now your parameterized test will have plenty of valid inputs to run against, and the "no parameters" error will be resolved.
内容的提问来源于stack exchange,提问作者Mohit Kanwar

