Test private methods in JUnit

background

When I was looking at the test code written in JUnit today, the test for the private method was not written. I made a sample that calls a private method, so I thought I'd post it.

Postscript For details, please refer to @ Kilisame's comment, but it is not an article that says that I tested it because I forgot to test private. It's hard to say that the caller's public test can't be done .... (I'd like you to understand), so I wrote it because I wanted to at least do a private test.

code

import static org.junit.Assert.*;

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

import org.junit.Test;

/**Sample class*/
public class private_Test {

	public class Sample{
		private int fuge(int i) {
			return i;
		}
	}
	
	@Test
	public void test() {
		
		Sample sample = new Sample();
		try {
			Method method = Sample.class.getDeclaredMethod("fuge", int.class);
			method.setAccessible(true);
			int rtn = (int)method.invoke(sample, 2);
			assertEquals(2,rtn);
			
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			//TODO auto-generated catch block
			e.printStackTrace();
		}
	}

}

You can get the method with the getDeclaredMethod () method, allow external access with setAccessible (true), and execute it with method.invoke ().

Recommended Posts

Test private methods in JUnit
Test private methods in JUnit
[Java] Test private methods with JUnit
About validation methods in JUnit
How to test private methods with arrays or variadic arguments in JUnit
If you want to satisfy the test coverage of private methods in JUnit
Output JUnit test report in Maven
Things to keep in mind when testing private methods in JUnit
How to filter JUnit Test in Gradle
Control test order in Junit4 with enumeration
Refactoring in JUnit
How to test private scope with JUnit
JUnit 5: How to write test cases in enum
Visualize test methods running in TestNG with listeners
Mixin test cases with JUnit 5 and default methods
Try JUnit test launch
Unit test with Junit.
Get the name of the test case in the JUnit test class
Implement test doubles in JUnit without using external libraries
[Java] JUnit4 test case example
Test Web API with junit
Dynamically call methods in JSF
Helper methods available in devise
Check Java9 Interface private methods
[JUnit] Test the thrown exception
Mock static methods in Mockito 3.4
[Spring Boot] Until @Autowired is run in the test class [JUnit5]
Implement Table Driven Test in Java 14
About methods often used in devise
How to run JUnit in Eclipse
Write test code in Spring Boot
JUnit 5 parameterization test is super convenient
Differences in split methods of StringUtils
Test API often used in AssertJ
Frequently used methods in Active Record
What are the rules in JUnit?
Test Spring framework controller with Junit
Ruby methods often used in Rails
How to test a private method in Java and partially mock that method