前言
SpringBoot集成Junit作为基本的测试操作,通常会由于某些原因,出现报错。本文就是对测试运行出现Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
报错进行总结,并不断更新。
前提
首先需要了解SpringBoot
集成Junit5
的基本操作。依赖和初步使用。
依赖
SpringBoot
版本以2.3.x为准。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
使用
在SpringBoot
项目的test
文件夹中创建测试类,类上添加注解@SpringBootTest
即可。如下:
@SpringBootTest
public class UserTest {
....
}
这就可以进行测试开发了。
源码
添加一个注解@SpringBootTest
就可以进行测试开发,那先了解下@SpringBootTest
这个注解
/**
1. 当没有定义特定的@ContextConfiguration(loader=…)时,使用SpringBootContextLoader作为默认的ContextLoader。
2. 当没有使用嵌套的@Configuration,并且没有指定显式类时,自动搜索@SpringBootConfiguration。
3. 允许使用properties属性定义自定义环境属性。
4. 允许使用args属性定义应用程序参数。
5. 提供对不同webEnvironment模式的支持,包括启动一个完全运行的web服务器监听一个已定义或随机端口的能力。
6. 注册一个TestRestTemplate和/或WebTestClient bean,用于使用完全运行的web服务器的web测试。
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
public @interface SpringBootTest {
/**
* Alias for {@link #properties()}.
* @return the properties to apply
*/
@AliasFor("properties")
String[] value() default {
};
/**
* Properties in form {@literal key=value} that should be added to the Spring
* {@link Environment} before the test runs.
* @return the properties to add
*/
@AliasFor("value")
String[] properties() default {
};
/**
* Application arguments that should be passed to the application under test.
* @return the application arguments to pass to the application under test.
* @see ApplicationArguments
* @see SpringApplication#run(String...)
* @since 2.2.0
*/
String[] args() default {
};
/**
* The <em>组件类</em> 用于加载一个{@link org.springframework.context.ApplicationContext ApplicationContext}
* 也可以被指定使用{@link ContextConfiguration#classes() @ContextConfiguration(classes=...)}.
* 如果没有显式类被定义,测试将寻找嵌套{@link Configuration @Configuration} 类,
* 然后返回到{@link SpringBootConfiguration @SpringBootConfiguration} search.
* @see ContextConfiguration#classes()
* 返回用于加载应用程序上下文的组件类
*/
Class<?>[] classes() default {
};
/**
* The type of web environment to create when applicable. Defaults to
* {@link WebEnvironment#MOCK}.
* @return the type of web environment
*/
WebEnvironment webEnvironment() default WebEnvironment.MOCK;
/**
* An enumeration web environment modes.
*/
enum WebEnvironment {
/**
* Creates a {@link WebApplicationContext} with a mock servlet environment if
* servlet APIs are on the classpath, a {@link ReactiveWebApplicationContext} if
* Spring WebFlux is on the classpath or a regular {@link ApplicationContext}
* otherwise.
*/
MOCK(false),
/**
* Creates a web application context (reactive or servlet based) and sets a
* {@code server.port=0} {@link Environment} property (which usually triggers
* listening on a random port). Often used in conjunction with a
* {@link LocalServerPort @LocalServerPort} injected field on the test.
*/
RANDOM_PORT(true),
/**
* Creates a (reactive) web application context without defining any
* {@code server.port=0} {@link Environment} property.
*/
DEFINED_PORT(true),
/**
* Creates an {@link ApplicationContext} and sets
* {@link SpringApplication#setWebApplicationType(WebApplicationType)} to
* {@link WebApplicationType#NONE}.
*/
NONE(false);
private final boolean embedded;
WebEnvironment(boolean embedded) {
this.embedded = embedded;
}
/**
* Return if the environment uses an {@link ServletWebServerApplicationContext}.
* @return if an {@link ServletWebServerApplicationContext} is used.
*/
public boolean isEmbedded() {
return this.embedded;
}
}
}
也就是说:
@SpringBootTest
注解在测试类运行时,会在项目中自动查询 @SpringBootConfiguration
注解。 同时@SpringBootApplication
中引用了 @SpringBootConfiguration
,所以找到 @SpringBootApplication
类就可以。
解决
报错Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
,无法找到@SpringBootConfiguration,
也就意味着没有找到@SpringBootApplication
。
所以可以进行以下操作解决:
- 启动类是否添加了注解
@SpringBootApplication
; - 测试类的包路径是否和启动类的包路径保持一致;
- 如果还不行,就显式指定
SpringBootTest
注解的class
属性。