简体   繁体   中英

Spring Boot: ReactiveCrudRepository is not being implemented by any bean

I plan to use Cassandra to save data reactively. To do that, I wrote the following interface:

@Repository
public interface ContributorStatRepository extends ReactiveCrudRepository<ContributorStat, Long> {
    Flux<ContributorStat> findByAkonId(String akonId);
}

The exception above is thrown:

com.example.sample.controller.ContributorStatControllerTest > shouldReturnBadRequestWithBlankContributorStat FAILED
    java.lang.IllegalStateException
        Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
            Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException
                Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException

Do you know why the appropiate bean for ContributorStatRepository is not being created?

I am using Spring boot 2.0.0.M7 and these dependencies:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('javax.xml.bind:jaxb-api:2.3.0')
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compile('org.springframework.boot:spring-boot-starter-data-cassandra-reactive')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('io.projectreactor:reactor-test')
}

Updated : Running test:

@Test
public void shouldReturnBadRequestWithBlankContributorStat() throws Exception {
    requestPayload = mapper.writeValueAsString(new ContributorStatDTO());

    this.mockMvc.perform(post(CONTRIBUTOR_STATS_ROUTE)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .content(requestPayload)).andDo(print())
            .andExpect(status().isBadRequest());
}

It seems you're using an @WebMvcTest annotated class to test this case (I'm not sure though, that part is missing from your question).

To test Spring WebFlux applications, you should use @WebFluxTest (see reference documentation ). Even if you do, the ContributorStatRepository bean won't be there since the web test slice will only consider the web parts of your application and you usually need to mock this one with @MockBean .

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