I am writing the integration tests with @WebFluxTest for my @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. Does somebody have an idea ?
I tried another approach using @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.
Thanks for your help.