0

I am trying to create a simple SpringBoot DB Unit repository test but I'm getting a:

NoSuchBeanDefinitionException: No qualifying bean of type 'example.ItemRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

My Gradle Dependencies

compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')

testCompile("junit:junit")
testCompile("org.assertj:assertj-core:3.8.0")
testCompile('org.springframework.boot:spring-boot-starter-test')

testCompile("org.dbunit:dbunit:2.4.9")
testCompile("com.github.springtestdbunit:spring-test-dbunit:1.0.0")

My Repository in item-repository/src/main/java/example/ItemRepository

@Component
public interface ItemRepository extends CrudRepository<Item, Long> {
}

My Repository Test in item-repository/src/test/java/example/ItemRepositoryTest

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RepositoryTestConfiguration.class)
@DirtiesContext
public class ItemRepositoryTest {

    @Autowired
    private ItemRepository repository;

    @Test
    @DatabaseSetup("Empty.xml")
    public void save() {
        // Given
        Item item = new Item

        // When
        Item response = repository.save(item);

        // Then
        assertThat(response.getId()).isNotNull();
    }

}

My Test Configuration in item-repository/src/main/test/example/configuration/RepositoryTestConfiguration

@Configuration
public class RepositoryTestConfiguration {
}

What do I need to include in my RepositoryTestConfiguration to get this to work?

Note: I keep my repositories in a seperate module to my Application class so I can't refer to that class in the test configuration

Eduardo
  • 6,900
  • 17
  • 77
  • 121
  • 1
    Where do you have `TestRepository`? – Adam Arold Nov 02 '17 at 14:06
  • @AdamArold I added file paths to the titles of each. All the above files are in the `item-repository` module which is seperate to my `item-application` module which contains my `Application` class – Eduardo Nov 02 '17 at 14:10
  • Do you use the `@SpringBootApplication` and the `@ComponentScan` annotations? – Adam Arold Nov 02 '17 at 14:12
  • @AdamArold I use `@SpringBootApplication` on my Application class, but I cant refer to that in my repository test due to dependency issues – Eduardo Nov 02 '17 at 14:13
  • Spring will only see your `RepositoryTestConfiguration` if it is a package which you included in your `@ComponentScan` – Adam Arold Nov 02 '17 at 14:15
  • In my test I have the annotation `@ContextConfiguration(classes = RepositoryTestConfiguration.class)` which should specify where it is – Eduardo Nov 02 '17 at 14:15
  • In hindsight I dont think its anything to do with DBUnit, its purely a spring test configuration thing – Eduardo Nov 02 '17 at 15:25

2 Answers2

0

You have to enable jpa repository like following annotation:

@EnableJpaRepositories(basePackages = {"com.hop.repository"})

on your RepositoryTestConfiguration class then your repositories beans can be autowired.

  • I get `Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!` If I do this – Eduardo Nov 02 '17 at 14:20
0

Oh ok as you are using spring boot, it enables auto configurations. So just use @SpringApplicationConfiguration instead of @ContextConfiguration.

  • I dont seem to have SpringApplicationConfiguration on my classpath, do you know what library + version I need? – Eduardo Nov 03 '17 at 08:44