MattGrommes
MattGrommes

Reputation: 12354

How do I test a class that has private methods, fields or inner classes?

How do I use JUnit to test a class that has internal private methods, fields or nested classes?

It seems bad to change the access modifier for a method just to be able to run a test.

Upvotes: 3258

Views: 1324936

Answers (30)

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21778

Define implementation class where the reason of public methods to be public is that they implement the clearly defined functionality, rather than if they are accessed from the outside of the class or not. If the functionality you want to test conveniently fits into a single method, make it into public function.

If you write the algorithm for exactly sum, there is nothing great in design the has a private sum() method returning sum minus one, to accomodate "one off" somewhere else.

The "production" class may have third party classes that require license dongle, call network services, produce non deterministic output, use specific hardware and the like. It may be totally no way how this could be unit tested.

Upvotes: 0

yoAlex5
yoAlex5

Reputation: 34341

Android has the @VisibleForTesting annotation from android.support.annotation package.

The @VisibleForTesting annotation indicates that an annotated method is more visible than normally necessary to make the method testable. This annotation has an optional otherwise argument that lets you designate what the visibility of the method should have been if not for the need to make it visible for testing. Lint uses the otherwise argument to enforce the intended visibility.

In practice, it means that you should make a method open for testing and the @VisibleForTesting annotation will show a warning.

For example

package com.mypackage;

public class ClassA {

    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static void myMethod() {

    }
}

And when you call ClassA.myMethod() within the same package(com.mypackage) you will see the warning.

Enter image description here

Upvotes: 6

WesternGun
WesternGun

Reputation: 12787

PowerMock.Whitebox is the best option I have seen, but when I read its source code, it reads private fields with reflection, so I think I have my answer:

  • test private internal states (fields) with PowerMock, or just reflection without the overhead of introducing another independency

  • for private methods: actually, the upvote for this question itself, and the huge number of comments and answers, shows that it is a very concurrent and controversial topic where no definite answer could be given to suit every circumstance. I understand that only the contract should be tested, but we also have coverage to consider. Actually, I doubt that only testing contracts will 100% make a class immune to errors. Private methods are those who process data in the class where it is defined and thus does not interest other classes, so we cannot simply expose to make it testable. I will try not to test them, but when you have to, just go for it and forget all the answers here. You know better your situation and restrictions than any other one on the Internet. When you have control over your code, use that. With consideration, but without overthinking.


After some time, when I reconsider it, I still believe this is true, but I saw better approaches.

First of all, Powermock.Whitebox is still usable.

And, Mockito Whitebox has been hidden after v2 (the latest version I can find with Whitebox is testImplementation 'org.mockito:mockito-core:1.10.19') and it has always been part of org.mockito.internal package, which is prone of breaking changes in the future (see this post). So now I tend not to use it.

In Gradle/Maven projects, if you define private methods or fields, there aren't any other ways than reflection to get access to them, so the first part stays true. But, if you change the visibility to "package private", the tests following the same structure in test package will have access to them. That is also another important reason why we are encouraged to create the same hierarchy in main and test package. So, when you have control over production code as well as tests, delete that private access modifier may be the best option for you because relatively it does not cause huge impact. And, that makes testing as well as private method spying possible.

@Autowired
private SomeService service; // With a package private method "doSomething()"

@Test
void shouldReturnTrueDoSomething() {
    assertThat(doSomething(input), is(true)); // Package private method testing
}

@Test
void shouldReturnTrueWhenServiceThrowsException() {
    SomeService spy = Mockito.spy(service); // Spying real object
    doThrow(new AppException()).when(spy).doSomething(input); // Spy package private method
    ...

}

When it comes to internal fields, in Spring you have ReflectionUtils.setField().

At last, sometimes we can bypass the problem itself: if there is a coverage requirement to meet, maybe you can move these private methods into an inner static class and ignore this class in Jacoco. I just found some way to ignore the inner class in Jacoco Gradle tasks. Another question.

Upvotes: 2

Rinke
Rinke

Reputation: 21

I think most answers are too crude. It depends on your code if you should test private methods or not.

I used private method testing in the past, and I did this by reflection. It works for me. I realize the problems with it, but for me it was the best solution.

I have a heavy application that simulates human behaviour in a population of over a million persons. Every person is represented by an object. The main purpose of the application is to follow how something (disease, information, ideas) spreads through the population.

For this I have methods that pass on the disease or information from one object to another. There is absolutely no reason to make these methods public, for the end user is not interested in one pass from person to person. The end user is only interested in the grand picture of how it spreads through the population. So those methods are private.

But I want to know for sure if the single act of passing one bit of information from one person to another is doing what it should. Testing it via public user interfaces is also not possible, because it's just not public, and I think it's awkward to make it public just for the purpose of testing. The end output of the application is defined by hundreds of millions of such single steps being performed all the time. I cannot test the end output either, because that has complicated stochastability involved, which makes it too unpredictable to test.

So the way for me to test the application is by testing a single step of passing information from one person to another person. These are all private methods. And so I use reflection to test that.

I'm telling this story to show that it's not that plain black and white story. It depends on your application what is best. Under some circumstances, testing private methods via reflection might be your best option.

If some people here know better solutions in my use case, I'd happily stand corrected of course....

Upvotes: 6

Abhishek Sengupta
Abhishek Sengupta

Reputation: 3301

Use this utility class if you are on Spring:

ReflectionTestUtils.invokeMethod(new ClassName(), "privateMethodName");

Upvotes: 7

Ercan
Ercan

Reputation: 2811

There is another approach to test your private methods.

If you "enable assertion" in run configurations then you can unit test your method inside the method itself. For example;

assert ("Ercan".equals(person1.name));
assert (Person.count == 2);

Upvotes: 1

AR1
AR1

Reputation: 5013

If you have a case where you really need to test a private method/class etc.. directly, you can use reflection as already mentioned in other answers. However if it comes to that, instead of dealing directly with reflection I'd rather use utility classes provided by your framework. For instance, for Java we have:

As per how to use them, you can find plenty of articles online. Here one that I particularly liked:

Upvotes: 3

Victor Grazi
Victor Grazi

Reputation: 16510

PowerMockito is made for this.

Use a Maven dependency:

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-core</artifactId>
        <version>2.0.7</version>
        <scope>test</scope>
    </dependency>

Then you can do

import org.powermock.reflect.Whitebox;
...
MyClass sut = new MyClass();
SomeType rval = Whitebox.invokeMethod(sut, "myPrivateMethod", params, moreParams);

Upvotes: 19

mohammad madani
mohammad madani

Reputation: 880

In C++: before including the class header that has a private function that you want to test.

Use this code:

#define private public
#define protected public

Upvotes: 7

The best and proper legal way to test a Java private method from a test framework is the @VisibleForTesting annotation over the method, so the same method will be visible for the test framework like a public method.

Upvotes: 3

Miladinho
Miladinho

Reputation: 174

If you are worried by not testing private methods as many of the posts suggest, consider that a code coverage tool will determine exactly how much of your code is tested and where it leaks, so it is quantifiably acceptable to do this.

Answers that direct the author of the question towards a 'workaround' are doing a massive disservice to the community. Testing is a major part of all engineering disciplines. You would not want to buy a car that is not properly tested, and tested in a meaningful way, so why would anyone want to buy or use software that is tested poorly? The reason people do this anyway is probably because the effects of badly tested software are felt way after the fact, and we don't usually associate them with bodily harm.

This is a very dangerous perception which will be difficult to change, but it is our responsibility to deliver safe products regardless of what even management is bullying us to do. Think the Equifax hack...

We must strive to build an environment that encourages good software engineering practices. This does not mean ostracizing the weak/lazy among us who do not take their craft seriously, but rather, to create a status quo of accountability and self reflection that would encourage everyone to pursue growth, both mentally and skillfully.

I am still learning, and may have wrong perceptions/opinions myself, but I do firmly believe that we need to hold ourselves accountable to good practices and shun irresponsible hacks or workarounds to problems.

Upvotes: 3

Rahul
Rahul

Reputation: 300

You can use PowerMockito to set return values for private fields and private methods that are called/used in the private method you want to test:

For example, setting a return value for a private method:

MyClient classUnderTest = PowerMockito.spy(new MyClient());

// Set the expected return value
PowerMockito.doReturn(20).when(classUnderTest, "myPrivateMethod", anyString(), anyInt());
// This is very important. Otherwise, it will not work
classUnderTest.myPrivateMethod();

// Setting the private field value as someValue:
Whitebox.setInternalState(classUnderTest, "privateField", someValue);

Then finally you can validate your private method under test is returning the correct value based on set values above by:

String msg = Whitebox.invokeMethod(obj, "privateMethodToBeTested", "param1");
Assert.assertEquals(privateMsg, msg);

Or

If the classUnderTest private method does not return a value, but it sets another private field then you can get that private field value to see if it was set correctly:

// To get the value of a private field
MyClass obj = Whitebox.getInternalState(classUnderTest, "foo");
assertThat(obj, is(notNull(MyClass.class))); // Or test value

Upvotes: 1

GROX13
GROX13

Reputation: 4765

I would suggest you refactoring your code a little bit. When you have to start thinking about using reflection or other kind of stuff, for just testing your code, something is going wrong with your code.

You mentioned different types of problems. Let's start with private fields. In case of private fields I would have added a new constructor and injected fields into that. Instead of this:

public class ClassToTest {

    private final String first = "first";
    private final List<String> second = new ArrayList<>();
    ...
}

I'd have used this:

public class ClassToTest {

    private final String first;
    private final List<String> second;

    public ClassToTest() {
        this("first", new ArrayList<>());
    }

    public ClassToTest(final String first, final List<String> second) {
        this.first = first;
        this.second = second;
    }
    ...
}

This won't be a problem even with some legacy code. Old code will be using an empty constructor, and if you ask me, refactored code will look cleaner, and you'll be able to inject necessary values in test without reflection.

Now about private methods. In my personal experience when you have to stub a private method for testing, then that method has nothing to do in that class. A common pattern, in that case, would be to wrap it within an interface, like Callable and then you pass in that interface also in the constructor (with that multiple constructor trick):

public ClassToTest() {
    this(...);
}

public ClassToTest(final Callable<T> privateMethodLogic) {
    this.privateMethodLogic = privateMethodLogic;
}

Mostly all that I wrote looks like it's a dependency injection pattern. In my personal experience it's really useful while testing, and I think that this kind of code is cleaner and will be easier to maintain. I'd say the same about nested classes. If a nested class contains heavy logic it would be better if you'd moved it as a package private class and have injected it into a class needing it.

There are also several other design patterns which I have used while refactoring and maintaining legacy code, but it all depends on cases of your code to test. Using reflection mostly is not a problem, but when you have an enterprise application which is heavily tested and tests are run before every deployment everything gets really slow (it's just annoying and I don't like that kind of stuff).

There is also setter injection, but I wouldn't recommended using it. I'd better stick with a constructor and initialize everything when it's really necessary, leaving the possibility for injecting necessary dependencies.

Upvotes: 16

Legna
Legna

Reputation: 1671

A quick addition to Cem Catikka's answer, when using ExpectedException:

Keep in mind that your expected exception will be wrapped in an InvocationTargetException, so in order to get to your exception you will have to throw the cause of the InvocationTargetException you received. Something like (testing private method validateRequest() on BizService):

@Rule
public ExpectedException thrown = ExpectedException.none();

@Autowired(required = true)
private BizService svc;


@Test
public void testValidateRequest() throws Exception {

    thrown.expect(BizException.class);
    thrown.expectMessage(expectMessage);

    BizRequest request = /* Mock it, read from source - file, etc. */;
    validateRequest(request);
}

private void validateRequest(BizRequest request) throws Exception {
    Method method = svc.getClass().getDeclaredMethod("validateRequest", BizRequest.class);
    method.setAccessible(true);
    try {
        method.invoke(svc, request);
    }
    catch (InvocationTargetException e) {
        throw ((BizException)e.getCause());
    }
 }

Upvotes: 3

Snicolas
Snicolas

Reputation: 38168

Today, I pushed a Java library to help testing private methods and fields. It has been designed with Android in mind, but it can really be used for any Java project.

If you got some code with private methods or fields or constructors, you can use BoundBox. It does exactly what you are looking for. Here below is an example of a test that accesses two private fields of an Android activity to test it:

@UiThreadTest
public void testCompute() {

    // Given
    boundBoxOfMainActivity = new BoundBoxOfMainActivity(getActivity());

    // When
    boundBoxOfMainActivity.boundBox_getButtonMain().performClick();

    // Then
    assertEquals("42", boundBoxOfMainActivity.boundBox_getTextViewMain().getText());
}

BoundBox makes it easy to test private/protected fields, methods and constructors. You can even access stuff that is hidden by inheritance. Indeed, BoundBox breaks encapsulation. It will give you access to all that through reflection, but everything is checked at compile time.

It is ideal for testing some legacy code. Use it carefully. ;)

Upvotes: 13

Steven Bluen
Steven Bluen

Reputation: 141

JML has a spec_public comment annotation syntax that allows you to specify a method as public during tests:

private /*@ spec_public @*/ int methodName(){
...
}

This syntax is discussed at 2.4 Privacy Modifiers and Visibility. There also exists a program that translates JML specifications into JUnit tests. I'm not sure how well that works or what its capabilities are, but it doesn't appear to be necessary since JML is a viable testing framework on its own.

Upvotes: 2

marc wellman
marc wellman

Reputation: 5886

I am not sure whether this is a good technique, but I developed the following pattern to unit test private methods:

I don't modify the visibility of the method that I want to test and add an additional method. Instead I am adding an additional public method for every private method I want to test. I call this additional method Test-Port and denote them with the prefix t_. This Test-Port method then simply accesses the according private method.

Additionally, I add a Boolean flag to the Test-Port method to decide whether I grant access to the private method through the Test-Port method from outside. This flag is then set globally in a static class where I place e.g. other global settings for the application. So I can switch the access to the private methods on and off in one place, e.g., in the corresponding unit test.

Upvotes: 3

simpatico
simpatico

Reputation: 11107

The answer from JUnit.org FAQ page:

But if you must...

If you are using JDK 1.3 or higher, you can use reflection to subvert the access control mechanism with the aid of the PrivilegedAccessor. For details on how to use it, read this article.

If you are using JDK 1.6 or higher and you annotate your tests with @Test, you can use Dp4j to inject reflection in your test methods. For details on how to use it, see this test script.

P.S. I'm the main contributor to Dp4j. Ask me if you need help. :)

Upvotes: 20

phareim
phareim

Reputation: 261

To test legacy code with large and quirky classes, it is often very helpful to be able to test the one private (or public) method I'm writing right now.

I use the junitx.util.PrivateAccessor-package for Java. It has lots of helpful one-liners for accessing private methods and private fields.

import junitx.util.PrivateAccessor;

PrivateAccessor.setField(myObjectReference, "myCrucialButHardToReachPrivateField", myNewValue);
PrivateAccessor.invoke(myObjectReference, "privateMethodName", java.lang.Class[] parameterTypes, java.lang.Object[] args);

Upvotes: 41

Richard Le Mesurier
Richard Le Mesurier

Reputation: 29762

Just two examples of where I would want to test a private method:

  1. Decryption routines - I would not want to make them visible to anyone to see just for the sake of testing, else anyone can use them to decrypt. But they are intrinsic to the code, complicated, and need to always work (the obvious exception is reflection which can be used to view even private methods in most cases, when SecurityManager is not configured to prevent this).

  2. Creating an SDK for community consumption. Here public takes on a wholly different meaning, since this is code that the whole world may see (not just internal to my application). I put code into private methods if I don't want the SDK users to see it - I don't see this as code smell, merely as how SDK programming works. But of course I still need to test my private methods, and they are where the functionality of my SDK actually lives.

I understand the idea of only testing the "contract". But I don't see one can advocate actually not testing code—your mileage may vary.

So my trade-off involves complicating the JUnit tests with reflection, rather than compromising my security and SDK.

Upvotes: 82

Jon
Jon

Reputation: 3602

I have used reflection to do this for Java in the past, and in my opinion it was a big mistake.

Strictly speaking, you should not be writing unit tests that directly test private methods. What you should be testing is the public contract that the class has with other objects; you should never directly test an object's internals. If another developer wants to make a small internal change to the class, which doesn't affect the classes public contract, he/she then has to modify your reflection based test to ensure that it works. If you do this repeatedly throughout a project, unit tests then stop being a useful measurement of code health, and start to become a hindrance to development, and an annoyance to the development team.

What I recommend doing instead is using a code coverage tool, such as Cobertura, to ensure that the unit tests you write provide decent coverage of the code in private methods. That way, you indirectly test what the private methods are doing, and maintain a higher level of agility.

Upvotes: 346

Thomas Hansen
Thomas Hansen

Reputation: 5513

In C# you could have used System.Reflection, though in Java I don't know. If you "feel you need to unit test private methods", my guess is that there is something else which is wrong...

I would seriously consider looking at my architecture again with fresh eyes...

Upvotes: 2

TofuBeer
TofuBeer

Reputation: 61536

As others have said... don't test private methods directly. Here are a few thoughts:

  1. Keep all methods small and focused (easy to test, easy to find what is wrong)
  2. Use code coverage tools. I like Cobertura (oh happy day, it looks like a new version is out!)

Run the code coverage on the unit tests. If you see that methods are not fully tested add to the tests to get the coverage up. Aim for 100% code coverage, but realize that you probably won't get it.

Upvotes: 30

Aaron
Aaron

Reputation: 3474

First, I'll throw this question out: Why do your private members need isolated testing? Are they that complex, providing such complicated behaviors as to require testing apart from the public surface? It's unit testing, not 'line-of-code' testing. Don't sweat the small stuff.

If they are that big, big enough that these private members are each a 'unit' large in complexity—consider refactoring such private members out of this class.

If refactoring is inappropriate or infeasible, can you use the strategy pattern to replace access to these private member functions / member classes when under unit test? Under unit test, the strategy would provide added validation, but in release builds it would be simple passthrough.

Upvotes: 11

Darren Greaves
Darren Greaves

Reputation: 3344

As many above have suggested, a good way is to test them via your public interfaces.

If you do this, it's a good idea to use a code coverage tool (like EMMA) to see if your private methods are in fact being executed from your tests.

Upvotes: 13

m.nguyencntt
m.nguyencntt

Reputation: 943

Here is my Lombok sample:

public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    Student student = new Student();

    Field privateFieldName = Student.class.getDeclaredField("name");
    privateFieldName.setAccessible(true);
    privateFieldName.set(student, "Naruto");

    Field privateFieldAge = Student.class.getDeclaredField("age");
    privateFieldAge.setAccessible(true);
    privateFieldAge.set(student, "28");

    System.out.println(student.toString());

    Method privateMethodGetInfo = Student.class.getDeclaredMethod("getInfo", String.class, String.class);
    privateMethodGetInfo.setAccessible(true);
    System.out.println(privateMethodGetInfo.invoke(student, "Sasuke", "29"));
}


@Setter
@Getter
@ToString
class Student {
    private String name;
    private String age;
    
    private String getInfo(String name, String age) {
        return name + "-" + age;
    }
}

Upvotes: 3

HilaB
HilaB

Reputation: 179

My team and I are using Typemock, which has an API that allows you to fake non-public methods.

Recently they added the ability to fake non-visible types and to use xUnit.

Upvotes: 1

Vishan
Vishan

Reputation: 57

If you are only using Mockito:

You can consider the private method as a part of public method being tested. You can make sure you cover all the cases in private method when testing public method.

Suppose you are a Mockito-only user (you are not allowed or don't want to use PowerMock or reflection or any such tools) and you don’t want to change the existing code or libraries being tested, this might be the best way.

The only thing you need to handle if you choose this way is the variables (user-defined objects) declared locally within private methods. If the private method depends on locally declared variable objects and their methods, make sure you declare those user-defined objects globally as private object instead of locally declared objects. You can instantiate these objects locally.

This allows you to mock these objects and inject them back to testing object. You can also mock (using when/then) their methods.

This will allow you test private method without errors when testing the public method.

Advantages

  1. Code coverage
  2. Able to test the complete private method.

Disadvantages

  1. Scope of the object—if you don't want the object to be exposed to other methods within same class, this might not be your way.
  2. You might end up testing the private method multiple times when invoked at different public methods and/or in the same method multiple times.

Upvotes: 1

Saša
Saša

Reputation: 4818

I want to share a rule I have about testing which particularly is related to this topic:

I think that you should never adapt production code in order to indulge easer writing of tests.

There are a few suggestions in other posts saying you should adapt the original class in order to test a private method - please red this warning first.

If we change the accessibility of a method/field to package private or protected, just in order to have it accessible to tests, then we defeat the purpose of existence of private access directive.

Why should we have private fields/methods/classes at all when we want to have test-driven development? Should we declare everything as package private, or even public then, so we can test without any effort?—I don't think so.

From another point of view: Tests should not burden performance and execution of the production application.

If we change production code just for the sake of easier testing, that may burden performance and the execution of the application in some way.

If someone starts to change private access to package private, then a developer may eventually come up to other "ingenious ideas" about adding even more code to the original class. This would make additional noise to readability and can burden the performance of the application.

With changing of a private access to some less restrictive, we are opening the possibility to a developer for misusing the new situation in the future development of the application. Instead of enforcing him/her to develop in the proper way, we are tempting him/her with new possibilities and giving him ability to make wrong choices in the future.

Of course there might be a few exceptions to this rule, but with clear understanding, what is the rule and what is the exception? We need to be absolutely sure we know why that kind of exception is introduced.

Upvotes: 9

d3rbastl3r
d3rbastl3r

Reputation: 513

I feel exactly the same ... change the access modifier for a method just to be able to run a test looks for me as a bad idea. Also in our company we had a lot of discussions about it and in my opinion, really nice way to test a private method is to using Java reflection or another framework which making the method testable. I did so multiple times for complex private methods and this helped me to keep the test small, readable and maintainable.

After I have read all the answers here, I just disagree with persons who say "if you need to test a private method, then there is a code smell" or even "don´t test private methods" ... so I have a little example for you:

Imagine I have a class with one public method and a couple of private methods:

public class ConwaysGameOfLife {

    private boolean[][] generationData = new boolean[128][128];

    /**
     * Compute the next generation and return the new state
     * Also saving the new state in generationData
     */
    public boolean[][] computeNextGeneration() {
        boolean[][] tempData = new boolean[128][128];

        for (int yPos=0; yPos<=generationData.length; yPos++) {
            for (int xPos=0; xPos<=generationData[yPos].length; xPos++) {
                int neighbors = countNeighbors(yPos, xPos);
                tempData[yPos][xPos] = determineCellState(neighbors, yPos, xPos);
            }
        }

        generationData = tempData;
        return generationData;
    }

    /**
     * Counting the neighbors for a cell on given position considering all the edge cases
     *
     * @return the amount of found neighbors for a cell
     */
    private int countNeighbors(int yPos, int xPos) {}

    /**
     * Determine the cell state depending on the amount of neighbors of a cell and on a current state of the cell
     *
     * @return the new cell state
     */
    private boolean determineCellState(int neighborsAmount, int yPos, int xPos) {}
}

So at least for the method "countNeighbors" I need to test eight edge cases and also some general cases (cells directly in the corners, cells directly on the edges of the matrix and cells somewhere in the middle). So if I am only trying to cover all the cases through the "computeNextGeneration" method and after refactoring, some tests are red, it is probably time consuming to identify the place where the bug is.

If I am testing "determineCellState" and "countNeighbors" separately and after refactoring and optimization, tests of "computeNextGeneration" and "determineCellState" are red, I am pretty sure that the error will be in the "determineCellState" method.

Also, if you write unit tests for those methods from the beginning, this tests will help you to develop the methods / the algorithms without a need to considering and wrapping other method calls and cases inside the public method. You can just write fast small tests covering your cases in the method ... for example if the test with the name "countNeighbors_should_return_right_amount_of_neighbors_for_the_right_top_corner_cell()" fails, then it is pretty clear where to look for the bug.

Upvotes: 2

Related Questions