作者的原创文章,转载须注明出处。原创文章归作者所有,欢迎转载,但是保留版权。对于转载了博主的原创文章,不标注出处的,作者将依法追究版权,请尊重作者的成果。

如何通过java反射的方式对java私有方法进行单元测试

待测试的私有方法:

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import springfox.documentation.service.ApiInfo;

import java.lang.reflect.Method;
public class SwaggerAPIPluginConfigTest {
SwaggerAPIPluginConfig swaggerAPIPluginConfig;
@BeforeClass
public void setUp(){
swaggerAPIPluginConfig = new SwaggerAPIPluginConfig();
}
//通过反射的方式对私有方法进行单元测试
@Test
public void testApiInfo() throws Exception {
Class<SwaggerAPIPluginConfig> swaggerAPIPluginConfig = SwaggerAPIPluginConfig.class;
Object instance = swaggerAPIPluginConfig.newInstance();
Method method = swaggerAPIPluginConfig.getDeclaredMethod("apiInfo", new Class[]{});
method.setAccessible(true);
ApiInfo result = (ApiInfo)method.invoke(instance,new Object[]{});
String expected="XXXXXXXXXX";
Assert.assertEquals(result.getTitle(),expected);
}
posted @ 2017-09-13 19:04  张永清  阅读(1826)  评论(0编辑  收藏  举报
作者的原创文章,转载须注明出处。原创文章归作者所有,欢迎转载,但是保留版权。对于转载了博主的原创文章,不标注出处的,作者将依法追究版权,请尊重作者的成果。