简体   繁体   中英

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

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.

This looks very similar to this . I'd suggest investigating your test configuration and adding it if appropriate.

A quote from Spring on @WebFluxTest

@Import annotation's arguments could be only @Configuration annotated classes - see the doc

You should delete your line with @Import , cause it gave you nothing here and misused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM