Springboot单元测试@RunWith注解

1.RunWith 注解

RunWith 就是一个运行器
可以在单元测试的时候,自动创建spring的应用上下文

2.正确使用

pom.xml


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-autoconfigure</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

3.测试用例

@RunWith(SpringRunner.class)
@SpringBootTest
public class RdsTest {

    private static final Logger LOG = LoggerFactory.getLogger(RdsTest.class);

    @Resource
    RdsClientHolder rdsClientHolder;

    @Resource
    RdsInstanceTypesMapper rdsInstanceTypesMapper;

    /**
     * 查询状态
     */
    @Test
    public void describeRds(){
        RdsClient rdsClient = rdsClientHolder.getClientByPinAlias("tech_prod");
        DescribeInstanceAttributesRequest request = new DescribeInstanceAttributesRequest();
        request.setRegionId("cn-north-1");
        request.setInstanceId("mysql-b706i2vpmt");
        DescribeInstanceAttributesResponse describeResponse = rdsClient.describeInstanceAttributes(request);
        LOG.info("describeResponse : {}",JsonUtils.toJSONString(describeResponse.getResult(), true));
    }
}	

4.springboot中的单元测试,不完整加载spring context,只加载依赖的内容

@Slf4j
@ActiveProfiles("dev")
@Import(value = {
        XingyunOperateServiceImpl.class,
        RestTemplateConfig.class,
        XingyunApiConfig.class
})
@EnableConfigurationProperties
@SpringBootTest(classes = XingyunOperateServiceTest.class)
public class XingyunOperateServiceTest {

    @Autowired
    private XingyunOperateService xingyunOperateService;

    @Test
    public void listSystems() {
        String erp = "111";
        List<System> systems = xingyunOperateService.getSystems(erp);
        log.info(systems.toString());
    }


    @Test
    public void listApps() {
        String erp = "hanboyuan";
        List<App> apps = xingyunOperateService.getApps("jdl-delta",erp);
        log.info(apps.toString());
    }
}
posted @   SpecialSpeculator  阅读(875)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示