这是我所拥有的:
@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/