I'm having a simple spring-data-jpa environemnt where I want to connect to a mysql database. Following code with the Entity:
@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 class (very simple)
@Configuration
@EnableAutoConfiguration
public class DemiConfiguration {}
Repository
public interface DemiRepository extends CrudRepository<DbRecord, Long> {}
and finally the test
@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();
}
}
So far so good. The test works on hsqldb but when I try to write to an mysql database and I create an application.properties into src/main/resources/application.properites with following values:
spring.datasource.url=jdbc:mysql://localhost/demi
spring.datasource.username=demi
spring.datasource.password=demi123
spring.datasource.driverClassName=com.mysql.jdbc.Driver
The test case ignores the properties and sticks to the default configuration with hsqldb.
I am using IntelliJ as IDE.
You need to add initializers = ConfigFileApplicationContextInitializer.class
to the @ContextConfiguration
. From its JavaDocs:
* {@link ApplicationContextInitializer} that can be used with the
* {@link ContextConfiguration#initializers()} to trigger loading of
* {@literal application.properties}.