SpringBoot+Junit Test

本文介绍如何使用Spring Boot进行单元测试,包括引入依赖包spring-boot-starter-test,并通过@RunWith和@SpringBootTest注解设置测试环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步引包:这个包集成了springboot做test所需要的所有

spring-boot-starter-test  (这个只是包名,可以到maven下载与自己版本相符的包)
我的Springboot版本是v1.5.2.RELEASE

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestSpringboot {
    @Autowired
    private UsersMapper usersMapper;

    @Test
    public void test()throws Exception{
        ......
    }

}



@SpringBootTest(classes = Application.class)

这一行当中Application.class是我的springboot的启动main入口。

有一个前提条件,springboot启动类必须

@SpringBootApplication 或者 @ComponentScan 注解否则找不到环境     





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值