【问题标题】:Spring Boot WebFluxTest test, failed to instantiate repository, specified class is an interfaceSpring Boot WebFluxTest 测试,无法实例化存储库,指定的类是一个接口
【发布时间】:2020-10-15 13:28:48
【问题描述】:

我正在用@WebFluxTest 为我的@RestController 编写集成测试。 这是我的课程:

@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());
    }

}

我得到以下异常:

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

我不明白为什么 Spring 不能注入存储库。 有人有想法吗?


我使用@SpringBootTest 尝试了另一种方法。这是我的新测试类:

@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());
    }

}

我得到了这个例外:

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)}

我不明白为什么 UserService 在应用程序上下文中不可用。

感谢您的帮助。

【问题讨论】:

    标签: java spring-boot integration-testing


    【解决方案1】:

    这看起来与this 非常相似。我建议调查您的测试配置并在适当时添加它。

    来自 Spring 的 quote @WebFluxTest

    使用此注解将禁用完全自动配置,而是仅应用与 WebFlux 测试相关的配置(即 @Controller、@ControllerAdvice、@JsonComponent、Converter/GenericConverter 和 WebFluxConfigurer bean,但不应用 @Component、@Service 或@Repository bean)。

    【讨论】:

      【解决方案2】:

      @Import 注释的参数只能是 @Configuration 注释类 - 请参阅 doc

      您应该删除带有@Import 的行,因为它在这里没有给您任何信息并被滥用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-20
        • 2017-10-23
        • 2019-08-04
        • 2020-05-09
        • 2018-11-27
        • 2018-08-09
        • 1970-01-01
        相关资源
        最近更新 更多