我正在尝试使用内存数据库(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>
存储库到测试类的 Autowiring 不起作用。我收到以下错误:
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。
最佳答案
我正在解决同样的问题,发现当对 CrudRepository、PagingAndSortingRepository 和 JpaRepository 使用自动配置时,不应该在 DataSource bean 中调用初始化脚本,因为内存数据库的内容会被 DatabasePopulator bean 覆盖。尝试如下配置代码:
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.datasource.init.DatabasePopulator;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
@Configuration
public class TestConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).build();
return db;
}
@Bean
public DatabasePopulator databasePopulator(DataSource dataSource) {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setContinueOnError(true);
populator.setIgnoreFailedDrops(true);
populator.addScript(new ClassPathResource("sql/create-db.sql"));
populator.addScript(new ClassPathResource("sql/insert-data.sql"));
try {
populator.populate(dataSource.getConnection());
} catch (SQLException ignored) {
}
return populator;
}
}
对于“可靠”数据库,例如 MySQL,可以显式提供 NULL 值:
@Bean
public DatabasePopulator databasePopulator() {
return null;
}
希望能解决您的问题
关于java - 用于集成测试的 Spring Data JPA 存储库接线不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35222941/