0

I'm trying to test service-class of reactive mongoDB application, created on SpringBoot.

I don't use embedded mongodb! I'm only trying to mock repository

This is reactive mongo repository:

package com.spb.budget_server.repository;

public interface EntryRepository extends ReactiveMongoRepository<Entry, String> {
  }

Repository, which should use it:

package com.spb.budget_server.repository.impl;

@AllArgsConstructor
@Repository
public class EntryRepositoryImpl {

    private EntryRepository entryRepository;
    //
}

And test class:

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {EntryRepositoryImpl.class, EntryRepository.class})   
public class EntryServiceTest {

    @Autowired
    private EntryRepositoryImpl entryRepositoryImpl;
       //
  }

Main configuration:

@Configuration
@EnableReactiveMongoRepositories()
public class MongoConfig extends AbstractReactiveMongoConfiguration {
  // some db-settings
}

May be It doesn't matter is that case, but test-configurations is:

 @TestConfiguration
 public class TestConfig {
    @Bean
    public EntryRepositoryImpl mockEntryRepositoryImpl() {
         return Mockito.mock(EntryRepositoryImpl.class);
      }
 }

And, after running test, I receive the following exception:

Error creating bean with name 'entryRepositoryImpl':

caused by:

  NoSuchBeanDefinitionException: No qualifying bean of type 'com.spb.budget_server.repository.EntryRepository' available: expected at least 1 bean which qualifies as autowire candidate

What am I doing wrong? Any ideas?

Jelly
  • 972
  • 1
  • 17
  • 40
  • Did you used `@EnableReactiveMongoRepositories`? – bilak Feb 23 '21 at 20:05
  • I use it in main configuration: @Configuration @EnableReactiveMongoRepositories() public class MongoConfig extends AbstractReactiveMongoConfiguration. I updated my question – Jelly Feb 23 '21 at 20:21
  • Try this `@EnableReactiveMongoRepositories(basePackageClasses = {EntryRepository.class})` – bilak Feb 23 '21 at 20:39
  • Ah or maybe if you are using that in tests, try to use [@DataMongoTest](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.html) on top of your class and then you don't need event `@SpringBootTest` – bilak Feb 23 '21 at 20:44
  • I add @EnableReactiveMongoRepositories(basePackageClasses = {EntryRepository.class}) in class MongoConfig, but receive the same exception... – Jelly Feb 23 '21 at 20:57
  • I don't use embedded mongoldb, i'm only trying to mock repository – Jelly Feb 23 '21 at 21:05

0 Answers0