Spring Boot Maven安装错误 - 找不到@SpringBootConfiguration

4
我正在尝试在创建项目Jar文件之前对我的Spring Boot项目执行"clean and install",但是我遇到了这个错误。
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

我是Spring Boot的新手,从未真正利用过它的测试功能。因此,我的测试类基本上是项目最初创建时的默认设置。

我的测试类:

   package com.Alex.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class WebAppApplicationTests {

    @Test
    void contextLoads() {
    }

}

文件目录

在此输入图片描述

3个回答

2

@RunWith(SpringRunner.class) 起作用了


0

对于您的测试类,您应该添加:

    @RunWith(SpringRunner.class)

另外,您应该考虑像这样更改测试的注释:

    @SpringBootTest(classes = { WebAppApplication.class })

Application.class 是你的 Spring Boot 项目的主类。


@Fenzox 我已将其更改为WebAppApplication,正如我之前提到的,这应该是您项目中包含主方法的类。 - William Andrés Bernal

0

问题在于无法找到您编写的测试方法的SpringBoot配置。 首先,类中提到的方法需要是公共的。

@Test
public void contextLoads() {
}

如果您将此作为主类的测试,并且在代码中标注了“WebAppApplication”,那么它应该可以正常工作。同时请确保在测试中使用了 @RunWith(SpringRunner.class)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接