待测试的私有方法:如何通过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);
}

相关文章:

  • 2023-03-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-02
  • 2022-01-07
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2021-11-10
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2021-07-15
相关资源
相似解决方案