SpringBoot Test测试类的使用

使用SpringBoot 测试类可在不需要启动程序时,即可使用。当你运行你的测试方法时他会自己启动程序调用所需使用到的mapper,service接口,实现方法。故而可在测试类中像编写正常service方法一样编写代码

pom依赖

		<!--Spring Boot 测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

编写测试类

import org.junit.jupiter.api.Test;
import org.sdyspx.stateartexam.manage.Application;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;


@SpringBootTest(classes = Application.class)
public class MainTest {
    @Resource
    XXXService xxxService;
    
    @Test
    public void testDemo() {
        // ...
    }
}

  1. 如果注解@SpringBootTest(classes = 启动类名称.class)中配置了项目启动类,则该测试类可以放在test.java下任何包中

  2. 如果注解@SpringBootTest没有配置里面的参数classes = Application.class,则需要确保test.java下的测试类包与启动类所在的包一致,即在test.java下也需要创建com.xunan.demo包,并将测试类放在该包下。
    在这里插入图片描述
    如果不这么弄当跑测试方法时会爆 Unable to find a @SpringBootConfiguration, you need to use @ContextConfigura错误

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值