今天在写一个类的单元测试的时候,想测试一下 一个类的私有方法
在网上找了一下,应该这样写:
@Test
@SuppressWarnings("JavaReflectionMemberAccess")
public void reqStatisticsTypeOne() throws Exception {
List<String> methodList = Arrays.stream(service.getClass().getDeclaredMethods()).map(Method::getName).collect(Collectors.toList());
methodList.forEach(System.out::println);
Method method = ServiceImpl.class.getDeclaredMethod("reqStatisticsTypeOne", String.class);
method.setAccessible(true);
JSONArray result7 = (JSONArray) method.invoke(service, "7");
result7.forEach(System.out::println);
JSONArray result6 = (JSONArray) method.invoke(service, "6");
result6.forEach(System.out::println);
}
嗯就是这样写的

本文介绍了一种测试Java类中私有方法的方法。通过反射机制获取并调用私有方法,展示了如何使用JUnit进行单元测试,并提供了具体的代码示例。
1093

被折叠的 条评论
为什么被折叠?



