Access the private method & attributes public class PrivateClassATest { @Test public void testAccessPublic() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException{ Method med = PrivateClassA.class.getMethod("getPublic", new Class[]{}); med.invoke(PrivateClassA.class.newInstance(), new Object[]{}); Method med2 = PrivateClassA.class.getDeclaredMethod("getPublic", new Class[]{}); med2.invoke(PrivateClassA.class.newInstance(), new Object[]{}); } @Test public void testAccessPrivate() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException{ Method med = PrivateClassA.class.getDeclaredMethod("getPrivate", new Class[]{}); med.setAccessible(true); med.invoke(PrivateClassA.class.newInstance(), new Object[]{}); } // Access the private attribute @SuppressWarnings("unchecked") @Test public void testAccessPrivateAttribute() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException{ Field f = PrivateClassA.class.getDeclaredField("map"); f.setAccessible( true ); Map<String, String> map = (Map<String, String>) f.get( new PrivateClassA() ); Assert.assertEquals( map.size(), 2 ); Iterator<String> keyIter = map.keySet().iterator(); Assert.assertEquals( keyIter.next(), "a" ); Assert.assertEquals( keyIter.next(), "b" ); } } public class PrivateClassA { private Map<String, String> map = new HashMap<String, String>(); public PrivateClassA(){ map.put("a", "a"); map.put("b", "b"); } public void getPublic(){ System.out.println("hi, I'm public method"); } @SuppressWarnings("unused") private void getPrivate(){ System.out.println("hi, I'm private method"); } } Access the private method & attributes itself and all its super classes