Springboot与Graphql错误“请考虑在配置中定义'{component name}'类型的bean”,

qij5mzcb  于 2023-01-02  发布在  Spring
关注(0)|答案(1)|浏览(132)

我正在使用Sping Boot 和Graphql以及rest。在添加Graphql组件时,我使用了这样的注解。
主计长

@Controller // <------ with this annotation
@EnableAutoConfiguration
class AController( 
    @Autowired val aRepository: ARepository,
    @Autowired val aService: AService
){
 ...
}

服务

@Service // <------ with this annotation
class AService (
{
 ...
}

储存库

@GraphQlRepository
interface ARepository: JpaRepository<A, Long> {

但得到了这些错误

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type '{component name}' in your configuration.

仅在使用test时发生

@WebMvcTest
@AutoConfigureMockMvc
internal class ControllerTest {
    @Test
    @WithMockUser
    fun healthCheck() {
        mockMvc.perform(get("/api/v1/healthcheck"))
            .andExpect(status().isOk).andExpect(
                content().string("healthy")
            ).andDo(print())
    }
}

我知道我可以使用ComponentSacn,但我想知道为什么会发生这种情况。因为这个包是和其他组件沿着放置的,而@ComponentScanner工作得很好。
我的结构是这样的。材料工作良好,但它不能扫描bakeryReview x1c 0d1x
我想我用的组件注解还可以,包结构也还可以。😭也许测试是问题所在?

qxsslcnc

qxsslcnc1#

我的错!!😂注解和结构都没有问题。对于那些看到这个问题的人来说,它是**WebMvcTest**。WebMvcTest需要被模拟的组件,因为它只注入了与Web相关的组件,如@Controller。因此它省略了其他组件,如@Service或@Repository。
我加了这些,效果还可以。

@MockBean
lateinit var aService: AService

@MockBean
lateinit var aRepository: ARepository

相关问题