lundi 27 août 2018

How do you get ApplicationContext to recognize CouchBaseRepositoyr beans from src/test/java?

I'm writing some unit tests for a reactive web application that uses couchbase repositories, however the ApplicationContext fails to load due to a NoSuchBeanDefinitionException being thrown when trying to @Autowire a ReactiveCouchbaseRepository.

From src/main/java, the application runs just fine, and I can send requests from PostMan and get valid responses. No configuration class is necessary. My application.properties is set up like this:

spring.couchbase.bootstrap-hosts=subdomain.domain.com
spring.couchbase.bucket.name=some_bucket_name
spring.couchbase.bucket.password=a_password
spring.couchbase.env.timeouts.connect=10000

I created a src/test/resources folder, marked it as the testing resources folder (in IntelliJ), and copied over the application.properties, but still the same exception is thrown. I also created a couchbase config class in the test folder that looks like this:

@Configuration
@EnableCouchbaseRepositories
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {


    @Value("${spring.couchbase.bootstrap-hosts}")
    private String hosts;

    @Value("${spring.couchbase.bucket.name}")
    private String bucketName;

    @Value("${spring.couchbase.bucket.password}")
    private String password;


    @Override
    protected List<String> getBootstrapHosts() {
        return Collections.singletonList(hosts);
    }

    @Override
    protected String getBucketName() {
        return bucketName;
    }

    @Override
    protected String getBucketPassword() {
        return password;
    }

}

My test class looks like this:

@RunWith(SpringRunner.class)
@WebFluxTest
public class ReactiveSpringApplicationTests {

    @Autowired
    WebTestClient webTestClient;

    @Test
    public void create_Test_Post_Event() throws Exception {

        EventData eventData = new EventData();
        eventData.setIdentifier("1111");

        webTestClient.post().uri("/ad/acc")
                .contentType(MediaType.APPLICATION_JSON_UTF8) // Set the media type of the body, as specified by the Content-Type header.
                .accept(MediaType.APPLICATION_JSON_UTF8) // Set the list of acceptable media types, as specified by the Accept header.
                .header("Corr", "0000")
                .body(Mono.just(eventData), EventData.class) // Set the body of the request to the given asynchronous Publisher.
                .exchange() // Perform the exchange without a request body.
                .expectStatus().isCreated()
                .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
                .expectBody(EventData.class);

    }
}

The following exceptions are thrown (full exception stack omitted for brevity, only the first and last exception shown):

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.amex.gar.accounts.repository.CouchBaseDocRepository<?, ?>' available: expected at least 1 bean which qualifies as autowire candidate.

(CouchBaseDocRepository extends ReactiveCouchBaseRepository)

How do I get the ApplicationContext to properly recognize the couchbase repositories from the test directory?

Aucun commentaire:

Enregistrer un commentaire