0
0
JUnittesting~20 mins

Testing private methods (should you?) in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Private Method Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why avoid testing private methods directly?

In unit testing with JUnit, why is it generally discouraged to test private methods directly?

ABecause private methods are automatically tested when public methods are tested
BBecause private methods are implementation details and testing them breaks encapsulation
CBecause private methods cannot be accessed by any test framework
DBecause private methods always contain bugs and should be rewritten instead
Attempts:
2 left
💡 Hint

Think about what private methods represent in object-oriented design.

Predict Output
intermediate
2:00remaining
JUnit test output for private method access

What will happen if you try to call a private method directly from a JUnit test class without using reflection?

JUnit
public class Calculator {
    private int add(int a, int b) {
        return a + b;
    }
}

public class CalculatorTest {
    @Test
    public void testAdd() {
        Calculator calc = new Calculator();
        int result = calc.add(2, 3); // Direct call to private method
    }
}
ACompilation error: add(int,int) has private access in Calculator
BTest passes and result is 5
CRuntime error: IllegalAccessException
DTest fails with NullPointerException
Attempts:
2 left
💡 Hint

Consider Java access modifiers and compile-time checks.

framework
advanced
3:00remaining
Using reflection to test private methods in JUnit

Which code snippet correctly uses Java reflection to invoke a private method calculate with no parameters in a class MathUtils inside a JUnit test?

A
Method method = MathUtils.class.getMethod("calculate");
Object result = method.invoke(new MathUtils());
B
Method method = MathUtils.class.getMethod("calculate");
method.setAccessible(true);
Object result = method.invoke(new MathUtils());
C
Method method = MathUtils.class.getDeclaredMethod("calculate");
Object result = method.invoke(new MathUtils());
D
Method method = MathUtils.class.getDeclaredMethod("calculate");
method.setAccessible(true);
Object result = method.invoke(new MathUtils());
Attempts:
2 left
💡 Hint

Remember that private methods require getDeclaredMethod and setAccessible(true).

assertion
advanced
2:00remaining
Correct assertion for private method result via reflection

After invoking a private method computeSum via reflection that returns an int, which JUnit assertion correctly verifies the result equals 10?

JUnit
Method method = MyClass.class.getDeclaredMethod("computeSum", int.class, int.class);
method.setAccessible(true);
int result = (int) method.invoke(new MyClass(), 4, 6);
AassertEquals(10, result);
BassertTrue(result == 10);
CassertEquals(result, 10);
DassertSame(10, result);
Attempts:
2 left
💡 Hint

Check the order of parameters in assertEquals.

🧠 Conceptual
expert
3:00remaining
Best practice for testing private methods in TDD

In Test-Driven Development (TDD), what is the best practice regarding testing private methods?

AMake private methods public temporarily to test them directly, then revert access modifiers
BWrite separate tests for private methods using reflection to ensure full coverage
CTest only the public interface; private methods are tested indirectly through public methods
DAvoid writing tests for private methods because they never contain logic
Attempts:
2 left
💡 Hint

Think about how TDD focuses on behavior rather than implementation.