0
0
JUnittesting~10 mins

@ValueSource for simple values in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to provide integer values to the test method using @ValueSource.

JUnit
@ParameterizedTest
@ValueSource([1])
void testWithInts(int number) {
    assertTrue(number > 0);
}
Drag options to blanks, or click blank then click option'
Aints = {1, 2, 3}
Bints = {0, -1, 2}
Cstrings = {"a", "b"}
Dints = {1, 2, 3, 4}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'strings' instead of 'ints' for integer values.
Including negative or zero values when the test expects positive numbers.
2fill in blank
medium

Complete the code to provide string values to the test method using @ValueSource.

JUnit
@ParameterizedTest
@ValueSource([1])
void testWithStrings(String word) {
    assertNotNull(word);
}
Drag options to blanks, or click blank then click option'
Astrings = {"apple", "banana", "cherry"}
Bints = {1, 2, 3}
Cdoubles = {1.0, 2.0}
Dchars = {'a', 'b'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ints' or other types instead of 'strings'.
Passing characters instead of strings.
3fill in blank
hard

Fix the error in the @ValueSource annotation to correctly provide double values.

JUnit
@ParameterizedTest
@ValueSource([1])
void testWithDoubles(double value) {
    assertTrue(value > 0.0);
}
Drag options to blanks, or click blank then click option'
Aints = {1, 2, 3}
Bdouble = {1.0, 2.5, 3.3}
Cdoubles = {1.0, 2.5, 3.3}
Dfloats = {1.0f, 2.5f}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'double' instead of 'doubles'.
Using 'floats' instead of 'doubles'.
4fill in blank
hard

Fill both blanks to create a test that uses @ValueSource to pass characters and asserts they are letters.

JUnit
@ParameterizedTest
@ValueSource([1])
void testWithChars(char letter) {
    assertTrue(Character.[2](letter));
}
Drag options to blanks, or click blank then click option'
Achars = {'a', 'b', 'c'}
BisDigit
CisLetter
Dchars = {'1', '2', '3'}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing digits instead of letters in chars.
Using 'isDigit' instead of 'isLetter' in the assertion.
5fill in blank
hard

Fill all three blanks to create a test using @ValueSource with strings and assert their length is greater than zero.

JUnit
@ParameterizedTest
@ValueSource([1])
void testStringLength([2] word) {
    assertTrue(word.[3]() > 0);
}
Drag options to blanks, or click blank then click option'
Astrings = {"test", "java", "junit"}
BString
Clength
Dints = {1, 2, 3}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ints' instead of 'strings' in @ValueSource.
Using wrong parameter type like 'int' instead of 'String'.
Calling a non-existent method instead of 'length()'.