对私有静态方法进行单测

对私有静态方法进行单测

1.被单测的类:

/**
 * Created by 58 on 2016-8-21.
 */
public class Student {

    private static String reading(String bookName) {
       return "student read " + bookName;
    }
}

2.使用junit方式

    @Test
    public void readingTest() {

        String expected = "student read ";
        String input = "Thinking in java";


        try {
            Method targetMethod = Student.class.getDeclaredMethod("reading", String.class);
            targetMethod.setAccessible(true);
            Object actual = targetMethod.invoke(Student.class, input);
            assertEquals(expected + input, actual);
        }catch (Exception e) {
            fail("单测异常");
        }
    }
原文地址:https://www.cnblogs.com/boothsun/p/5792714.html