0
0
JUnittesting~10 mins

Why fast tests enable frequent runs in JUnit - Test Your Understanding

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

Complete the code to mark the test method with JUnit 5 annotation.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class CalculatorTest {
    [1]
    void testAddition() {
        int sum = 2 + 3;
        assertEquals(5, sum);
    }
}
Drag options to blanks, or click blank then click option'
A@AfterAll
B@BeforeEach
C@Test
D@Disabled
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @Test
Forgetting to add any annotation
Using @Disabled which skips the test
2fill in blank
medium

Complete the code to assert that the result equals 10.

JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

@Test
void testMultiplication() {
    int result = 2 * 5;
    [1](10, result);
}
Drag options to blanks, or click blank then click option'
AassertEquals
BassertTrue
CassertNull
DassertFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertTrue with equality expression instead of assertEquals
Using assertNull which checks for null values
3fill in blank
hard

Fix the error in the test method to make it run correctly.

JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

@Test
void testDivision() {
    int result = 10 / 2;
    assertEquals(5, [1]);
}
Drag options to blanks, or click blank then click option'
Aresult
B10
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a literal number instead of the variable 'result'
Swapping expected and actual arguments
4fill in blank
hard

Fill both blanks to create a fast test that runs multiple times.

JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.RepeatedTest;

@RepeatedTest([1])
void testFastAddition() {
    int sum = 1 + 1;
    assertEquals([2], sum);
}
Drag options to blanks, or click blank then click option'
A5
B2
C3
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong number of repetitions
Asserting wrong expected sum
5fill in blank
hard

Fill all three blanks to create a test that fails fast on wrong input.

JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

@Test
void testFailFast() {
    int input = [1];
    int expected = [2];
    int actual = input * 2;
    assertEquals([3], actual);
}
Drag options to blanks, or click blank then click option'
A3
B4
C6
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching expected and actual values
Using input that does not match expected output