1

My test class looks like:

@WebFluxTest(controllers = IndexContoller.class)
@Import(UserServiceImpl.class)
class IndexContollerTest {
    private static final String PATH = "/";
    private static final String ADD_USER = "/signup";
    @MockBean
    private UserRepo userRepo;
    @Autowired
    private WebTestClient webClient;
    @Autowired
    private ReactiveUserDetailsService userService;

    @Test
    void saveUser() throws Exception {
    Mockito.when(userRepo.save(any())).thenReturn(Mono.just(new User()));
    webClient.post()
            .uri(ADD_USER)
            .contentType(MediaType.APPLICATION_JSON)
            .body(BodyInserters.fromObject(new User()))
            .exchange()
            .expectStatus()
            .is2xxSuccessful();}
    }

and after i ran it it falls with

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'skillGroupRepository' defined in com.gmail.qwertygoog.roadmap.repository.SkillGroupRepository defined in @EnableR2dbcRepositories declared on RoadmapApplication: Cannot resolve reference to bean 'r2dbcEntityTemplate' while setting bean property 'entityOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'r2dbcEntityTemplate' available

Basically it searches for different service bean's dependency and falls after cant instantiate it.I also tried to add @SpringExtension withought any difference in output. Any suggestions?

tarmogoyf
  • 298
  • 3
  • 17
  • Fast fixing: UserServiceImpl implements ReactiveUserDetailsService and it uses UserRepo extends ReactiveCrudRepository – tarmogoyf Aug 23 '21 at 12:18
  • 1
    Ditch `@Import(UserServiceImpl.class` and use `@MockBean` on the `UserService` instead of the repository. You are trying to do an `@SpringBootTest` (integration test) with an `@WebFluxTest` and shoehorning it into this. – M. Deinum Aug 23 '21 at 12:23
  • I removed @Import, repository, mocked service and caught the same exception. It's still tries to load extra beans during test. – tarmogoyf Aug 23 '21 at 12:38
  • 2
    Do you have annotations on your `@SpringBootApplication` annotated class? If so move to a separate `@Configuration` class. – M. Deinum Aug 23 '21 at 12:43
  • I imported TestConfig class annotated with TestConfiguration & EnableR2dbcRepositories. It starts to ask extra beans like UserRepo. – tarmogoyf Aug 24 '21 at 07:58
  • What worked for me, is combo of AutoConfigureWebTestClient & SpringBootTest. But I'm still confused cause it's definitely waste of resources and I must be missed a concept of WebFluxTest config. – tarmogoyf Aug 24 '21 at 07:59
  • 2
    Your `@SpringBootApplication` class shouldn't contain anything else, add that to a different `@Configuraiton` class, which you don't `@Import` else it behaves the same. If it is on the `@SpringBootApplication` annotated class, it has to be included, if not that the test infrastructure has a chance to stop things from being configured. – M. Deinum Aug 24 '21 at 08:06
  • Oops, i misunderstood your previous comment, thx. – tarmogoyf Aug 24 '21 at 08:08
  • 1
    As M.Deinum stated, it was all due to annotation on main class (@EnableR2dbcRepository) in my case. After shifting it on PersistenceConfig.class it starts working as expected. Thanks! – tarmogoyf Aug 24 '21 at 08:18

0 Answers0