w3resource

Java Testing Private methods with reflection: ExampleClass demonstration

Java Unit Test: Exercise-8 with Solution

Write a Java program to explore strategies for testing private methods in a class.

Sample Solution:

Java Code:

// ExampleClass.java

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ExampleClass {

    private int addTwoNumbers(int a, int b) {
        return a + b;
    }

    // Public method that calls the private method
    public int performAddition(int a, int b) {
        return addTwoNumbers(a, b);
    }

    // Testing private method using reflection
    public static int testPrivateMethod(ExampleClass instance, int a, int b)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // Obtain the Class object for the class containing the private method
        Class clazz = instance.getClass();

        // Specify the name and parameter types of the private method
        Class[] parameterTypes = {int.class, int.class};
        Method privateMethod = clazz.getDeclaredMethod("addTwoNumbers", parameterTypes);

        // Make the private method accessible for testing
        privateMethod.setAccessible(true);

        // Invoke the private method on the provided instance
        return (int) privateMethod.invoke(instance, a, b);
    }

    public static void main(String[] args) {
        ExampleClass example = new ExampleClass();

        // Test the public method that calls the private method
        int result = example.performAddition(3, 5);
        System.out.println("Result from public method: " + result);

        try {
            // Test the private method using reflection
            int privateMethodResult = testPrivateMethod(example, 3, 5);
            System.out.println("Result from private method: " + privateMethodResult);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

Sample Output:

Result from public method: 8
Result from private method: 8

Explanation:

In the exercise above,

  • ExampleClass: Defines a class with a private method (addTwoNumbers) and a public method (performAddition) that calls the private method.
  • testPrivateMethod Method: Uses reflection to test the private method. This method takes an instance of "ExampleClass", the parameters for the private method, and uses reflection to invoke the private method.
  • main Method: Demonstrates how to test the public and private methods. The public method is tested directly, and the private method is tested using the testPrivateMethod utility.

Flowchart:

Flowchart: Java Testing Private methods with reflection: ExampleClass demonstration
Flowchart: Java Testing Private methods with reflection: ExampleClass demonstration

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Java Custom Assertion Example: Providing Meaningful feedback in tests.
Next: Testing Java Singleton class for Multi-Threading with JUnit.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/unittest/java-unittest-exercise-8.php