【发布时间】:2016-05-15 08:36:47
【问题描述】:
我正在尝试使用内存数据库(HSQL)为我的 TemplateRepository 编写集成测试。
public interface TemplateRepository extends CrudRepository<TemplateEntity, String> {
TemplateEntity findByIdAndTemplateName(String id, String templateName);
void deleteByIdAndTemplateName(String cifId, String templateName);
}
这是目前为止的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class TemplatesRepositoryTest {
@Autowired
private TemplateRepository templateRepository;
private EmbeddedDatabase db;
@Before
public void setUp() {
db = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("sql/create-db.sql")
.addScript("sql/insert-data.sql")
.build();
}
@Test
public void test1() { }
}
哪个在使用这个上下文配置:
@Configuration
public class TestConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.HSQL)
.addScript("sql/create-db.sql")
.addScript("sql/insert-data.sql")
.build();
return db;
}
}
以及应用类:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
以及声明的依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
存储库到测试类的自动装配不起作用。我收到以下错误:
11:25:57.300 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5f3a4b84] to prepare test instance [TemplatesRepositoryTest@4eb7f003]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'TemplatesRepositoryTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private TemplateRepository TemplatesRepositoryTest.templateRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [TemplateRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我已经尝试了找到的所有相关主题中的解决方案,但似乎没有一个可以解决问题。有人能指出我在这里缺少什么吗?
我正在使用 SpringBoot 1.3.1.RELEASE 和 SpringDataJpa 1.9.2.RELEASE。
【问题讨论】:
-
尝试将 @ComponentScan 添加到您的 Application 类中,以便 Spring 找到 TemplateRepository bean。 docs.spring.io/spring-boot/docs/current/reference/html/…
-
我在任何地方都没有看到
@EnableJpaRepositories。 -
您的项目结构如何?也许存储库接口与您的 SpringBootApplication 不在同一个包中?
-
@Alan Hay 我试过了,没有任何改变
-
将@EnableJpaRepositories(basePackages={""}) 添加到您的配置类中
标签: java spring jpa spring-data integration