简体   繁体   English

Spring Boot WebFluxTest 测试,无法实例化存储库,指定类为接口

[英]Spring Boot WebFluxTest test, failed to instantiate repository, specified class is an interface

I am writing the integration tests with @WebFluxTest for my @RestController .我正在使用@WebFluxTest为我的@RestController编写集成测试。 Here are my classes:这是我的课程:

@RestController
@RequestMapping("/usager")
public class UsagerController {

    @Autowired
    private UsagerService usagerService;

    @GetMapping
    public Usager getUsager() {
        return usagerService.create();
    }

}
@Service
public class UsagerService implements CrudService<Usager, Integer> {

    @Autowired
    private UsagerRepository usagerRepository;

    @Override
    public JpaRepository<Usager, Integer> getRepository() {
        return usagerRepository;
    }

    @Override
    public Usager create() {
        return new Usager();
    }

}
@Repository
public interface UsagerRepository extends JpaRepository<Usager, Integer>, JpaSpecificationExecutor<Usager> {

}
@ExtendWith(SpringExtension.class)
@WebFluxTest(UsagerController.class)
@Import({ UsagerService.class, UsagerRepository.class })
@Tag(TestCase.INTEGRATION)
public class UsagerControllerIT {

    @Autowired
    private WebTestClient wtc;

    @Test
    public void getUsager_returnUsager() {
        ResponseSpec rs = wtc.get().uri("/usager").exchange();

        rs.expectStatus().isOk();
        rs.expectHeader().contentType(MediaType.APPLICATION_JSON);
        rs.expectBody(Usager.class).isEqualTo(new Usager());
    }

}

I get the following exception:我收到以下异常:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.dsi.bibliosys.biblioback.repository.UsagerRepository]: Specified class is an interface

I don't understand why Spring can't inject the repository.我不明白为什么 Spring 不能注入存储库。 Does somebody have an idea ?有人有想法吗?


I tried another approach using @SpringBootTest .我使用@SpringBootTest尝试了另一种方法。 Here is my new test class :这是我的新测试类:

@ExtendWith(SpringExtension.class)
@SpringBootTest
@Tag(TestCase.INTEGRATION)
public class UsagerController02IT {

    @Autowired
    private UsagerController usagerController;

    @Test
    public void getUsager_returnUsager() {
        WebTestClient wtc = WebTestClient.bindToController(usagerController).build();

        ResponseSpec rs = wtc.get().uri("/usager").exchange();

        rs.expectStatus().isOk();
        rs.expectHeader().contentType(MediaType.APPLICATION_JSON);
        rs.expectBody(Usager.class).isEqualTo(new Usager());
    }

}

I get this exception:我得到这个例外:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.dsi.bibliosys.biblioback.controller.UsagerController': Unsatisfied dependency expressed through field 'usagerService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dsi.bibliosys.biblioback.service.entity.UsagerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I don't understand why UserService is not available in the application context.我不明白为什么UserService在应用程序上下文中不可用。

Thanks for your help.谢谢你的帮助。

This looks very similar to this .这看起来非常相似, 这个 I'd suggest investigating your test configuration and adding it if appropriate.我建议调查您的测试配置并在适当时添加它。

A quote from Spring on @WebFluxTest一个报价从春天就@WebFluxTest

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to WebFlux tests (ie @Controller, @ControllerAdvice, @JsonComponent, Converter/GenericConverter, and WebFluxConfigurer beans but not @Component, @Service or @Repository beans).使用此注释将禁用完全自动配置,而是仅应用与 WebFlux 测试相关的配置(即 @Controller、@ControllerAdvice、@JsonComponent、Converter/GenericConverter 和 WebFluxConfigurer bean,但不应用 @Component、@Service 或 @Repository bean)。

@Import annotation's arguments could be only @Configuration annotated classes - see the doc @Import注释的参数只能是@Configuration注释类 - 请参阅文档

You should delete your line with @Import , cause it gave you nothing here and misused.你应该用@Import删除你的行,因为它在这里没有给你任何东西并且被滥用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring boot - 如何在HTTP控制器处理程序中避免“无法实例化[java.util.List]:指定的类是一个接口”? - spring boot - how to avoid “Failed to instantiate [java.util.List]: Specified class is an interface” in HTTP controller handler? 如何解决 Spring-Boot Repository 的“Specified class is an interface”错误? - How to resolve "Specified class is an interface" error for Spring-Boot Repository? 无法实例化UriInfo指定的类是接口 - Failed to instantiate UriInfo Specified class is an interface Spring插入错误无法实例化[java.util.Map]:指定的类是一个接口 - Spring insert error Failed to instantiate [java.util.Map]: Specified class is an interface 如何使用 Spring 安全 @Secured 注释测试 Spring 引导 @WebFluxTest - How to test Spring Boot @WebFluxTest with Spring Security @Secured annotation 带有SpringBoot的GraphQLTester无法实例化xxx指定的class是一个接口 - GraphQLTester with SpringBoot Failed to instantiate xxx specified class is an interface 接口的 Spring Boot 存储库 - Spring Boot Repository of an interface Spring Boot Data JPA 无法在 MockMVC 测试中自动装配存储库接口 - Spring Boot Data JPA cannot autowire repository interface in MockMVC test Spring Boot测试:无法实例化内部配置类 - Spring boot test: Unable to instantiate inner configuration class 升级 Spring 启动后无法自动装配 @Repository 注释接口 - Failed to Autowire @Repository annotated interface after upgrading Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM