springboot的测试类

本文介绍了如何在SpringBoot项目中配置和使用测试类。通过引入`spring-boot-starter-test`依赖,我们可以利用`@RunWith(SpringRunner.class)`和`@SpringBootTest`注解在容器环境中进行测试。示例中展示了如何创建并测试一个服务类,包括数据的创建和批量保存操作。

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

springboot的测试类

引入jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!--test表明只能在test包下才可以用的jar,在非test包下不可用-->
            <scope>test</scope>
        </dependency>

编写测试类

@RunWith(SpringRunner.class) //作用:让当前类在容器环境下进行测试
@SpringBootTest(classes = NtWebdcApplication.class)//作用:声明当前类是springboot的测试类并且获取入口类上的相关信息 NtWebdcApplication是入口类类名
public class TestBean {
    @Autowired
    TestUuidService testUuidService;

    @Test
    public void testSelectAllProvinces() {
        List<TestUuid> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            TestUuid testUuid = new TestUuid();
            testUuid.setName("test" + i);
            list.add(testUuid);
        }
        testUuidService.saveBatch(list);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值