---
title: "How can I test a class that has private methods, fields, or inner classes?"  
description: "How can I test a class that has private methods, fields, or inner classes?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-07-22  
canonical: https://www.mindstick.com/forum/159219/how-can-i-test-a-class-that-has-private-methods-fields-or-inner-classes  
category: "java"  
tags: ["java", "class"]  
reading_time: 4 minutes  

---

# How can I test a class that has private methods, fields, or inner classes?

How can I [test](https://yourviews.mindstick.com/story/1427/explosive-facts-about-trinity-test-world-s-first-nuclear-bomb) a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) that has [private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best), [fields](https://www.mindstick.com/forum/159126/sql-query-for-all-tables-and-fields-in-an-oracle-db), or [inner classes](https://www.mindstick.com/interview/2728/what-are-different-types-of-inner-classes)?

## Replies

### Reply by Aryan Kumar

There are a few ways to test a class that has private methods, fields, or inner classes.

- **Use reflection.** Reflection is a feature of Java that allows you to access and modify the behavior of objects at runtime. You can use reflection to access and invoke private methods and fields, and to create instances of inner classes.

To test a class that has private methods, fields, or inner classes using reflection, you can use the following steps:

1. Create an instance of the class that you want to test.
2. Use the `getDeclaredMethods()` method to get a list of the class's declared methods.
3. For each method in the list, check if the method is private. If it is, then use the `invoke()` method to invoke the method.
4. Repeat steps 2 and 3 for the class's declared fields and inner classes.

Here is an example of how to test a class that has private methods using reflection:

```plaintext
import java.lang.reflect.Method;

public class MyClass {

  private String name;

  private void setName(String name) {
    this.name = name;
  }

  private String getName() {
    return this.name;
  }

  public static void main(String[] args) {
    MyClass myClass = new MyClass();

    // Get the list of declared methods
    Method[] methods = myClass.getClass().getDeclaredMethods();

    // For each method in the list, check if the method is private
    for (Method method : methods) {
      if (method.isPrivate()) {
        // Invoke the method
        Object result = method.invoke(myClass, null);

        // Do something with the result
        System.out.println(result);
      }
    }
  }
}
```

- **Use a testing framework that supports reflection.** There are a number of testing frameworks available for Java that support reflection. These frameworks can make it easier to test classes that have private methods, fields, or inner classes.

Some of the popular testing frameworks that support reflection include JUnit, TestNG, and Spock.

To test a class that has private methods, fields, or inner classes using a testing framework that supports reflection, you can use the following steps:

1. Create a test case for the class that you want to test.
2. Use the testing framework's API to access and invoke the class's private methods, fields, and inner classes.

Here is an example of how to test a class that has private methods using the JUnit testing framework:

```plaintext
import org.junit.Test;

public class MyClassTest {

  @Test
  public void testSetName() {
    MyClass myClass = new MyClass();

    // Use the JUnit API to invoke the private setName() method
    myClass.setName("John Doe");

    // Verify that the name property is set to the correct value
    assertEquals("John Doe", myClass.getName());
  }
}
```

- **Use a mocking framework.** A mocking framework is a tool that allows you to create fake objects that can be used to test your code. You can use a mocking framework to create fake objects for classes that have private methods, fields, or inner classes.

Some of the popular mocking frameworks available for Java include Mockito, EasyMock, and JMockit.

To test a class that has private methods, fields, or inner classes using a mocking framework, you can use the following steps:

1. Create a mock object for the class that you want to test.
2. Use the mocking framework's API to configure the mock object so that it can answer your test assertions.
3. Use the mocking framework's API to invoke the class's private methods, fields, and inner classes.

Here is an example of how to test a class that has private methods using the Mockito mocking framework:

```plaintext
import org.mockito.Mock;
import org.mockito.Mockito;
import org.junit.Test;

public class MyClassTest {

  @Mock
  private MyClass myClass;

  @Test
  public void testSetName() {

    // Configure the mock object so that it returns "John Doe" when
    // the getName() method is invoked
    Mockito.when(myClass.getName()).thenReturn("John Doe");

    // Invoke the private setName() method
    myClass.setName("Jane Doe");
```


---

Original Source: https://www.mindstick.com/forum/159219/how-can-i-test-a-class-that-has-private-methods-fields-or-inner-classes

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
