【发布时间】:2015-11-20 04:30:39
【问题描述】:
我有带有配置的 Spring Boot 应用程序:
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true, prePostEnabled=true)
@EnableJpaRepositories(basePackages = "com.site.data.repository")
@EntityScan(basePackages = "com.site.data.entity")
@ComponentScan(basePackages = "com.*")
@SpringBootApplication
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
在resources文件夹中有application.properties文件:
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driverClassName=...
#...
问题:
我尝试创建一个新类,它将向数据库添加一些条目。我认为最好的解决方案是制作一个 JUnit 测试类。因此,如果有人想提供我的数据库,他可以简单地运行这个特定的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebSecurityConfig.class)
public class DataFeeder {
@Autowired
MyRepository myRepository;
@Test
public void feedDB() {
MyEntity entity = new MyEntity();
myRepository.saveAndFlush(entity);
}
}
但是这个配置不起作用。当然,所有存储库都在项目中运行良好。但是在运行测试期间,我收到所有表都不存在的消息。例如对于 SOME_TABLE:
Table "SOME_TABLE" not found; SQL statement:
我看了很多关于如何测试JpaRepository的教程,但是我不想做测试,它会添加一些项目并在测试结束后将其删除。我只是想在数据库中保存一些数据(但在 @Test 函数中)
我的配置有什么问题?
【问题讨论】:
-
尝试使用
@SpringApplicationConfiguration而不是@ContextConfiguration -
@Jens 不幸的是它并没有改变情况。我更改了日志级别,第一条消息是:
hibernate.properties not found。所以我怀疑Spring在这个测试类中没有看到application.properties。编辑:没有 hibernate.properites 很好,因为在正常的应用程序启动中它也会发生。 -
@Jens 对不起。你说的对。你的建议是正确的。当我(正确)更改为
@WebIntegrationTest时,一切正常。如果你愿意,请回答,我会接受的。 -
已添加我的评论作为答案。
标签: java spring junit spring-boot spring-data-jpa