Hi @djohn98390536
For private static method you can use Reflection API like this -
@Test
void checkMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
String input = "Hello, world!";
InputStream is = new ByteArrayInputStream(input.getBytes());
TestUtil testUtil = new TestUtil();
Method privateMethod = TestUtil.class.getDeclaredMethod("StreamToString",InputStream.class);
privateMethod.setAccessible(true);
// invoke the private method for test
Object s = privateMethod.invoke(testUtil, is);
assertEquals(s.toString(),"Hello, world!");
}
Hi @djohn98390536,
in case your method is private, then you shouldn't write a unit test for it at all. This is considered bad practice by most. Instead, focus on testing your public methods that will cover the logic in your private methods as well.
Good luck,
Daniel