spring - 带有 Spring Boot 2.1 WebMvcTest 的 Kotlin - 缺少 bean

标签 spring spring-boot testing kotlin

这是我所拥有的:

@RunWith(SpringRunner::class)
@ActiveProfiles("unit-test")
@WithUserDetails
@WebMvcTest(MyController::class)
class MyControllerTest {

    @MockBean
    lateinit var service: MyService

    @Autowired
    lateinit var mvc: MockMvc

但我不断收到类似于以下的异常:

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 

每次我添加依赖项时,例如:

    @MockBean
    lateinit var anotherBean: AnotherBean

抛出另一个丢失的 bean (有点无穷无尽)。

在 Kotlin 中是否有合适的解决方案?

最佳答案

只需添加注解@SpringBootTest,来自spring guides :

The @SpringBootTest annotation tells Spring Boot to go and look for a main configuration class (one with @SpringBootApplication for instance), and use that to start a Spring application context. You can run this test in your IDE or on the command line (mvn test or gradle test) and it should pass.

关于spring - 带有 Spring Boot 2.1 WebMvcTest 的 Kotlin - 缺少 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944886/

相关文章:

mysql - Spring boot,执行自定义查询

ruby-on-rails - Rails - 使用功能测试测试 JSON API

testing - 我可以得到一个常规页面来重定向子目录中的页面吗?

java - Bean 类的无效属性 'profileManager'。 Bean 属性 'profileManager' 不可写或具有无效的设置方法

java - 使用 Spring Boot 的基于名称的虚拟主机

java - 使用 Spring Boot 读取资源文件夹中可用的文件

rest - 在 codeception 中将文件发送到 Restful 服务

java - 第一次在 Spring 上使用 QueryDSL MongoDB

java - Spring:注入(inject) Scala 常量

java - LogstashTcpSocketAppender 未将日志发送到 Logstash