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