0
0
JUnittesting~10 mins

Testing private methods (should you?) 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 tests a public method.

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

class CalculatorTest {
    @Test
    void testAdd() {
        Calculator calc = new Calculator();
        int result = calc.add(2, 3);
        assertEquals([1], result);
    }
}
Drag options to blanks, or click blank then click option'
A6
B0
C23
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong expected value in assertEquals.
Confusing the order of parameters in assertEquals.
2fill in blank
medium

Complete the code to use reflection to access a private method named 'calculate' in a test.

JUnit
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {
    @Test
    void testPrivateCalculate() throws Exception {
        Calculator calc = new Calculator();
        Method method = Calculator.class.getDeclaredMethod("calculate", int.class, int.class);
        method.setAccessible(true);
        int result = (int) method.invoke(calc, 4, 5);
        assertEquals([1], result);
    }
}
Drag options to blanks, or click blank then click option'
A9
B20
C45
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Not setting the method accessible before invoking.
Using the wrong expected value in assertEquals.
3fill in blank
hard

Fix the error in the test code that tries to test a private method directly.

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

class CalculatorTest {
    @Test
    void testPrivateMethod() {
        Calculator calc = new Calculator();
        // This line causes a compile error because 'calculate' is private
        int result = calc.[1](3, 7);
        assertEquals(10, result);
    }
}
Drag options to blanks, or click blank then click option'
Acalculate
Bsubtract
Cadd
Dmultiply
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call private methods directly in test code.
Not understanding access modifiers in Java.
4fill in blank
hard

Fill both blanks to complete the explanation about testing private methods.

JUnit
/* It is generally recommended to test [1] methods instead of private methods directly.
 * Private methods should be tested indirectly through [2] methods.
 */
Drag options to blanks, or click blank then click option'
Apublic
Bprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to test private methods directly.
Confusing public and private method roles.
5fill in blank
hard

Fill all three blanks to complete the code snippet that uses reflection safely to test a private method.

JUnit
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {
    @Test
    void testPrivateMethodReflection() throws Exception {
        Calculator calc = new Calculator();
        Method method = Calculator.class.getDeclaredMethod("[1]", int.class, int.class);
        method.[2](true);
        int result = (int) method.invoke(calc, [3], 8);
        assertEquals(13, result);
    }
}
Drag options to blanks, or click blank then click option'
Acalculate
BsetAccessible
C5
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong method name.
Forgetting to set the method accessible.
Passing wrong argument values.