我有一个简单的spring-data-jpa environemnt,我想连接到mysql数据库。以下代码与实体:
@Entity
public class DbRecord extends AbstractPersistable<Long> {
private String name;
public DbRecord(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
配置类(非常简单)
@Configuration
@EnableAutoConfiguration
public class DemiConfiguration {}
存储库
public interface DemiRepository extends CrudRepository<DbRecord, Long> {}
最后是测试
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration(classes = DemiConfiguration.class)
public class SimpleTest {
@Autowired
DemiRepository repository;
@Test public void testInsert() {
DbRecord record = new DbRecord("test");
repository.save(record);
Iterable<DbRecord> records = repository.findAll();
}
}
到目前为止一切顺利。测试适用于hsqldb,但是当我尝试写入mysql数据库并在src / main / resources / application.properites中创建一个application.properties时,其值如下:
spring.datasource.url=jdbc:mysql://localhost/demi
spring.datasource.username=demi
spring.datasource.password=demi123
spring.datasource.driverClassName=com.mysql.jdbc.Driver
测试用例忽略属性并使用hsqldb保持默认配置。
我使用IntelliJ作为IDE。
答案 0 :(得分:1)
您需要将initializers = ConfigFileApplicationContextInitializer.class
添加到@ContextConfiguration
。来自它的JavaDocs:
* {@link ApplicationContextInitializer} that can be used with the
* {@link ContextConfiguration#initializers()} to trigger loading of
* {@literal application.properties}.