user3298823
user3298823

Reputation: 302

Autoconfigure ReactiveCrudRepos in integration tests

I'm having some difficulty in writing some Integration tests. I have something like so

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Service.class)
public class ServiceTest {
    @Rule
    public PostgreSQLContainer postgres = new PostgreSQLContainer();

    @Autowired
    Service Service;

    @Before
    public void setUp() {
        PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
                .host(postgres.getHost())
                .port(postgres.getFirstMappedPort())  // optional, defaults to 5432
                .username(postgres.getUsername())
                .password(postgres.getPassword())
                .database(postgres.getDatabaseName())  // optional
                .build());

        Resource resource = new ClassPathResource("sql.sql");

        Mono<PostgresqlConnection> mono = connectionFactory.create();
        mono.map(connection -> connection
                .createStatement(Helpers.asString(resource))
                .execute()).block();

    }

    @Test
    public void test() {
        Request Request = new Request();
        request.setName("name");
        Mono<Item> itemMono = Service.createNewHub(hubRequest);
        Item item = itemMono.block();

        Assert.assertEquals(1L, 1L);

    }
}

And my Service.class looks like the below

@Service
public class Service {

    private Repository repository;

    public Service(Repository repository) {
        this.repository = repository;
    }

    public Flux<Item> getAllItems() {
            return repository.findAll();
    }
}

And my repo

@Repository
public interface Repository extends ReactiveCrudRepository<Item, Integer>  {

}

My error is the following

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.Repository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

While all of the code I have written in the application is able to be injected fine, when it comes to the ReactiveCrudRepos, I am not having any luck on getting their instantiated object. What do I need to do to have the implementations created and injected?

Upvotes: 1

Views: 390

Answers (1)

Nikolas
Nikolas

Reputation: 44388

As long as you use @SpringBootTest(classes = Service.class), the other beans are not loaded into the application context. The annotation element class of the annotation is described as:

The component classes to use for loading an ApplicationContext.

Remove the element and use the @SpringBootApplication as is:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Service.class)
public class ServiceTest { /* code */ }

Remember, using this way is testing an application context completely different from what will be run on the production environment. It should be a last resort.


Moreover, avoid naming the interface Repository when there exists the Spring annotation @Repository itself. I would personally prefer:

@Repository
public interface ItemRepository extends ReactiveCrudRepository<Item, Integer>  {

}

Upvotes: 1

Related Questions