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());
}
}
原创:做时间的朋友

浙公网安备 33010602011771号