【记录个人springboot项目启动常见问题及解决方案】

本文详细解答了SpringBoot项目中常见的启动失败、依赖问题及测试类配置,包括启动类放置规则、@EnableAutoConfiguration排除和测试类依赖设置。确保包结构一致,教你如何正确设置并测试SpringBoot应用。
摘要由CSDN通过智能技术生成

写在最前面:

本篇文章能解决的问题:springboot项目从入口类启动失败;测试类无法测试;报错提示
无法找到bean注入等等。。这些错误多半是由于项目结构错误,依赖没引好,注解放错原因导致的。
本文将持续更新。。

test类和springboot启动类结构

注意:springboot程序入口类和test类在同一包结构底下:com.huang
test类和启动类结构

关于springboot启动类

启动类要放在 com.huang的这种package下面,不可以直接放在java包底下,不然启动报错。
另:@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})这个注解是让启动时允许不连接DB

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class RabbitMQAPP {

    public static void main(String[] args) {
        SpringApplication.run(RabbitMQAPP.class,args);
    }
}

关于测试类

springboot项目测试类所需maven依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/>
    </parent>
   <dependencies>
       <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>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

测试类要点总结:

  1. 测试类的所需的pom依赖直接照抄
  2. 测试类注解如下代码快所示:注意注解写法: SpringBootTest (classes = RabbitMQAPP.class) class是springboot启动类的class!!
  3. test包的结构一定要和springboot启动类的结构一样:都要有com.huang

@SpringBootTest(classes = RabbitMQAPP.class)
@RunWith(SpringRunner.class)
@Slf4j
public class PractiseTest {

    @Autowired
    private Publisher publisher;


    @Test
    public void testSpringEvent(){
        publisher.sendMsg();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值