Search code examples
springspring-bootspring-testspring-boot-test

Avoid repository db connection on tests with @SpringBootTest


I have a test class annotated with @SpringBootTest, it also has @AutoConfigureWebTestClient to test some router functions. All the handlers in the router have @Autowired repositories that go to couchbase. I use @MockBean to test one of the repositories, but not I'm not using the others.

Router

@Configuration
public class Router {

 public Router(Handler1 handler1, Handler2 handler2){
  this.handler1 = handler1;
  this.handler2 = handler2;
}

@Bean
public RouterFunction<ServerResponse> route() {
 return RouterFunctions()
                .route(GET("/path1").and(accept(MediaType.APPLICATION_JSON)), handler1::findAll)
                .andRoute(GET("/path2").and(accept(MediaType.APPLICATION_JSON)), handler2::findAll);
}

Handler class

@Service
public class Handler1{
 Repository1 repository;

 @Autowired
 public Handler1(Repository1 repository){
  this.repository = repository;
 }
}

Test Class

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureWebTestClient
public class Handler1Test {
  @Autowired
  private WebTestClient webClient;
  
  @MockBean
  private Repository1 repositoryMock;

  @Test
  public void findAll() throws IOException {
   Mockito.when(repositoryMock.findall()).thenReturn(Mono.just(""));
   webClient.get()
                .uri("/path1")
                .accept(MediaType.APPLICATION_JSON)
                .exchange()
                .expectStatus().isOk();
  }
}

Whenever I run the test, it connects to the database. But the test uses the mock correctly.

The logs show this:

2020-06-19 15:36:45.295  INFO 41724 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Couchbase repositories in DEFAULT mode.
2020-06-19 15:36:45.525  INFO 41724 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 229ms. Found 21 Couchbase repository interfaces.

How can I stop the connections from happening?

EDIT: If I add this annotation still wants to connect

@EnableAutoConfiguration(exclude = {CouchbaseAutoConfiguration.class,
 CouchbaseDataAutoConfiguration.class,
 CouchbaseRepositoriesAutoConfiguration.class,
 CouchbaseReactiveDataAutoConfiguration.class,
 CouchbaseReactiveRepositoriesAutoConfiguration.class})

Solution

  • All my repositories extend from ReactiveCouchbaseRepository, so in my case excluding CouchbaseReactiveRepositoriesAutoConfiguration.class did the job:

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
    @EnableAutoConfiguration(exclude = {CouchbaseAutoConfiguration.class,
        CouchbaseDataAutoConfiguration.class,
        CouchbaseRepositoriesAutoConfiguration.class,
        CouchbaseReactiveDataAutoConfiguration.class,
        CouchbaseReactiveRepositoriesAutoConfiguration.class})
    @AutoConfigureWebTestClient
    public class GeographicAreaHandlerTest {...}
    

    But it wasn't enough. I had to annotate my CouchbaseConfiguration class with

    @Profile({"default", "development"})
    public class CouchbaseConfiguration extends AbstractReactiveCouchbaseConfiguration {}
    

    And enable a profile called testing in my tests.