My problem is that i'm not able to create test which use in memory database. All the times got errors, for example:
Caused by: java.lang.IllegalStateException: Unable to retrieve @EnableAutoConfiguration base packages
How could i do the test with H2 inmemory database?
Entity class:
@Entity
@Table(name = "names")
public class Names{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "name_id")
private int id;
@Column(name = "name")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Repository class:
@Repository("namesRepository")
public interface NamesRepository extends JpaRepository<Names, Long> {
Names findByName(String name);
List<Names> findAll();
}
Database config class:
@Configuration
public class DatabaseConfig {
@Bean
@ConfigurationProperties(prefix="spring.dbProfileService")
@Primary
public DataSource createProfileServiceDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Autowired
public JdbcTemplate createJdbcTemplate_ProfileService(DataSource profileServiceDS) {
return new JdbcTemplate(profileServiceDS);
}
}
application.yml
spring:
application:
name: app
dbProfileService:
driverClassName: org.postgresql.Driver
url: "jdbc:postgresql://localhost/postgres"
password: "postgres"
username: "postgres"
testOnBorrow: false
testWhileIdle: false
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1
max-active: 15
max-idle: 10
max-wait: 8000
Test class:
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootConfiguration
public class NamesTest {
@Autowired
private NamesRepository names;
@Test
public void firsTest(){
}
}
Gradle dependencies:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.2.RELEASE'
// https://mvnrepository.com/artifact/com.h2database/h2
testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile(group: 'org.postgresql', name: 'postgresql', version: '42.2.2')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-cassandra', version: '2.0.0.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.1.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-validator', version: '4.0.2.GA'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.0.1.RELEASE'
}