有没有编写单元测试,以如果某个bean是未能创造确保春季启动API不上手的方式。例如:无法创建数据源的bean。
此代码应为你做它:
@RunWith(SpringRunner.class)
@SpringBootTest
public class AnyAppNameApplicationTests {
@Test
public void contextLoads() {
}
}
从文档:
注释可以在运行春季启动基于测试的测试类指定。提供了与普通的Spring TestContext框架上面的以下功能:
- 使用SpringBootContextLoader作为默认的ContextLoader没有定义特定
@ContextConfiguration(loader=...)
时。- 自动搜索
@SpringBootConfiguration
当不使用嵌套@Configuration
,并没有显式指定的类。- 允许自定义环境属性来使用特性属性来定义。
- 为不同webEnvironment模式,包括开始完全运行的Web服务器侦听定义或随机端口的能力支持。
- 对于注册在使用一个完全运行Web服务器的Web测试使用TestRestTemplate和/或WebTestClient豆。
如果您正在使用JUnit 4,不要忘了也
@RunWith(SpringRunner.class)
添加到您的测试,否则注释将被忽略。如果您在使用JUnit 5,有没有必要添加相当于@ExtendWith(SpringExtension)
为@SpringBootTest
和其他@…Test
注解已经与它注释。