0
0
JUnittesting~10 mins

Answer interface for dynamic responses 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 create a JUnit test method that asserts two strings are equal.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.[1];

public class SampleTest {
    @Test
    void testStrings() {
        String expected = "hello";
        String actual = "hello";
        [1](expected, actual);
    }
}
Drag options to blanks, or click blank then click option'
AassertNull
BassertFalse
CassertTrue
DassertEquals
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue instead of assertEquals causes the test to check a boolean, not equality.
Using assertNull or assertFalse does not compare two strings.
2fill in blank
medium

Complete the code to create a dynamic test name using JUnit 5's @DisplayName annotation.

JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

public class DynamicNameTest {
    @Test
    @DisplayName("Test for user: [1]")
    void testUser() {
        // test code here
    }
}
Drag options to blanks, or click blank then click option'
A"John"
BJohn
CuserName
D"userName"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causes a compilation error.
Using a variable name without quotes does not work in @DisplayName.
3fill in blank
hard

Fix the error in the code to correctly implement a dynamic test with JUnit 5's @TestFactory.

JUnit
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DynamicTests {
    @TestFactory
    Stream<DynamicTest> dynamicTests() {
        return Stream.of(
            DynamicTest.dynamicTest("Test 1", () -> [1])
        );
    }
}
Drag options to blanks, or click blank then click option'
AassertTrue(1 > 2)
BassertEquals(2, 1 + 2)
CassertEquals(2, 1 + 1)
DassertNull(null)
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong expected values causes test failure.
Using assertions that do not compile or are invalid in lambda.
4fill in blank
hard

Fill both blanks to create a parameterized test method that uses @ValueSource with integers.

JUnit
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.[1];

public class ParamTest {
    @ParameterizedTest
    @[2](ints = {1, 2, 3})
    void testNumbers(int number) {
        // test code
    }
}
Drag options to blanks, or click blank then click option'
AValueSource
BCsvSource
DMethodSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using @CsvSource or @MethodSource without proper format causes errors.
Mismatching import and annotation names.
5fill in blank
hard

Fill all three blanks to create a dynamic test stream that tests if strings are not empty.

JUnit
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StringTests {
    @TestFactory
    Stream<DynamicTest> dynamicTestsForStrings() {
        String[] inputs = {"apple", "", "banana"};
        return Stream.of(inputs).map(input ->
            DynamicTest.dynamicTest("Test: " + input, () -> {
                [1].assertTrue(!input.[2](), "String is empty");
            })
        );
    }
}
Drag options to blanks, or click blank then click option'
Aorg.junit.jupiter.api.Assertions
BisEmpty
CassertTrue
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Calling assertTrue without the Assertions class causes errors.
Using length() instead of isEmpty() changes the logic.
Mixing up assertTrue and assertFalse.