我正在尝试创建一个简单的SpringBoot DB单元存储库测试,但是我得到了一个:
NoSuchBeanDefinitionException:没有“example.ItemRepository”类型的合格bean可用:预期至少有一个bean可以作为自动测试候选。依赖性注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的等级依赖
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();
}
}我的测试配置在item-repository/src/main/test/example/configuration/RepositoryTestConfiguration中
@Configuration
public class RepositoryTestConfiguration {
}我需要在我的RepositoryTestConfiguration中包含哪些内容才能让它正常工作?
注意:我将我的存储库保存在一个独立的模块中,与我的Application类分开,所以我不能在测试配置中引用该类
发布于 2017-11-02 14:18:03
您必须启用jpa存储库,如下所示:
@EnableJpaRepositories(basePackages = {"com.hop.repository"})
在您的RepositoryTestConfiguration类中,您的存储库bean可以自动存储。
发布于 2017-11-02 16:54:56
哦,好的,因为你正在使用弹簧引导,它支持自动配置。所以,只需使用@SpringApplicationConfiguration而不是@ContextConfiguration。
https://stackoverflow.com/questions/47077260
复制相似问题