0
0
JUnittesting~5 mins

@ValueSource for simple values in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @ValueSource annotation in JUnit?

@ValueSource is used to provide a set of simple values to a parameterized test method. It helps run the same test multiple times with different inputs.

Click to reveal answer
beginner
Which types of simple values can @ValueSource provide in JUnit?

@ValueSource supports primitive types like int, long, double, short, byte, char, boolean, and also String arrays.

Click to reveal answer
beginner
How do you write a parameterized test method using @ValueSource for integers 1, 2, and 3?
  @ParameterizedTest
  @ValueSource(ints = {1, 2, 3})
  void testWithInts(int number) {
    // test code using 'number'
  }
Click to reveal answer
intermediate
What happens if you pass an unsupported type to @ValueSource?

JUnit will fail to run the test and throw an error because @ValueSource only supports specific simple types. You must use supported types only.

Click to reveal answer
intermediate
Can @ValueSource be combined with other sources like @CsvSource in JUnit?

No, @ValueSource cannot be combined with other sources on the same test method. Use only one source annotation per parameterized test method.

Click to reveal answer
Which of these is a valid use of @ValueSource in JUnit?
A@ValueSource(strings = {"apple", "banana"})
B@ValueSource(objects = {new Object(), new Object()})
C@ValueSource(lists = {{1,2}, {3,4}})
D@ValueSource(dates = {"2023-01-01"})
What annotation must be used together with @ValueSource to run parameterized tests?
A@ParameterizedTest
B@Test
C@BeforeEach
D@RepeatedTest
Which of these types is NOT supported by @ValueSource?
Aint
BString
Cboolean
DList<String>
How many source annotations can you use on a single parameterized test method?
AThree
BTwo
COne
DUnlimited
What will happen if you pass unsupported types to @ValueSource?
ATest runs normally
BJUnit throws an error
CValues are ignored
DTest is skipped
Explain how to use @ValueSource to run a test multiple times with different simple values.
Think about how to feed different inputs to the same test method.
You got /4 concepts.
    List the simple value types supported by @ValueSource in JUnit.
    Focus on primitive types and strings.
    You got /2 concepts.