对私有静态方法进行单测

对私有静态方法进行单测

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("单测异常");
        }
    }

 

posted @ 2016-08-21 14:38  做个有梦想的咸鱼  阅读(479)  评论(0编辑  收藏  举报