Challenge - 5 Problems
JUnit @MethodSource Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of @MethodSource supplying multiple arguments
Given the following JUnit 5 test code using @MethodSource, what will be the output when the test runs?
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; public class CalculatorTest { static Stream<org.junit.jupiter.params.provider.Arguments> provideAddData() { return Stream.of( org.junit.jupiter.params.provider.Arguments.of(1, 2, 3), org.junit.jupiter.params.provider.Arguments.of(3, 4, 7), org.junit.jupiter.params.provider.Arguments.of(5, 5, 10) ); } @ParameterizedTest @MethodSource("provideAddData") void testAdd(int a, int b, int expected) { assertEquals(expected, a + b); } }
Attempts:
2 left
💡 Hint
Check the method signature and the parameter types in the test method.
✗ Incorrect
The @MethodSource method returns a Stream of Arguments with three sets of parameters matching the test method's parameters. The test method parameters match the Arguments count and types. The @ParameterizedTest annotation is correctly used. Therefore, all tests pass.
❓ assertion
intermediate1:30remaining
Correct assertion for @MethodSource test with multiple parameters
Which assertion correctly verifies the output of a method that adds two integers, when using @MethodSource to supply test data?
JUnit
static Stream<org.junit.jupiter.params.provider.Arguments> data() {
return Stream.of(
org.junit.jupiter.params.provider.Arguments.of(2, 3, 5),
org.junit.jupiter.params.provider.Arguments.of(4, 5, 9)
);
}
@ParameterizedTest
@MethodSource("data")
void testSum(int x, int y, int expected) {
// Which assertion is correct here?
}Attempts:
2 left
💡 Hint
Remember the order of parameters in assertEquals(expected, actual).
✗ Incorrect
The correct order for assertEquals is assertEquals(expected, actual). Option D uses this order correctly. Option D reverses the order, which is allowed but not recommended as it can confuse test failure messages. Options B and D use boolean assertions which are less clear and less informative on failure.
🔧 Debug
advanced2:00remaining
Identify the cause of NoSuchMethodException in @MethodSource
A test using @MethodSource("testData") fails with NoSuchMethodException. What is the most likely cause?
JUnit
public class SampleTest { @ParameterizedTest @MethodSource("testData") void testExample(int input) { // test code } static Stream<Integer> testdata() { return Stream.of(1, 2, 3); } }
Attempts:
2 left
💡 Hint
Check spelling and capitalization of the method name in @MethodSource.
✗ Incorrect
JUnit requires the method name in @MethodSource to exactly match the factory method name, including case. Here, @MethodSource("testData") does not match static method testdata(), causing NoSuchMethodException.
❓ framework
advanced2:00remaining
Behavior of @MethodSource with non-static factory method
What happens if a @MethodSource factory method is non-static in a JUnit 5 test class?
JUnit
public class DemoTest { @ParameterizedTest @MethodSource("dataProvider") void test(int val) { // test code } Stream<Integer> dataProvider() { return Stream.of(10, 20); } }
Attempts:
2 left
💡 Hint
Consider the lifecycle of the test class and how JUnit accesses factory methods.
✗ Incorrect
By default, JUnit 5 requires @MethodSource factory methods to be static. Non-static methods cause exceptions unless the test class uses @TestInstance(Lifecycle.PER_CLASS), which allows instance methods as sources.
🧠 Conceptual
expert2:30remaining
Understanding parameter matching in @MethodSource with Arguments
Consider a @MethodSource factory method returning Stream with three parameters per Arguments. The test method has two parameters. What will happen when the test runs?
JUnit
static Stream<org.junit.jupiter.params.provider.Arguments> provideData() {
return Stream.of(
org.junit.jupiter.params.provider.Arguments.of(1, 2, 3),
org.junit.jupiter.params.provider.Arguments.of(4, 5, 9)
);
}
@ParameterizedTest
@MethodSource("provideData")
void testSum(int a, int b) {
// test code
}Attempts:
2 left
💡 Hint
Check if the number of parameters in the test method matches the number of arguments supplied.
✗ Incorrect
JUnit requires the number of parameters in the test method to exactly match the number of arguments provided by the factory method. Mismatch causes ParameterResolutionException.