Моя проблема в том, что я не могу создать тест, который используется в базе данных памяти.Все время возникали ошибки, например:
Причина: java.lang.IllegalStateException: Невозможно получить базовые пакеты @EnableAutoConfiguration
Как я могу выполнить тест с базой данных H2?
Класс сущности:
@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("namesRepository")
public interface NamesRepository extends JpaRepository<Names, Long> {
Names findByName(String name);
List<Names> findAll();
}
Класс конфигурации базы данных:
@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
Класс теста:
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootConfiguration
public class NamesTest {
@Autowired
private NamesRepository names;
@Test
public void firsTest(){
}
}
Зависимости Gradle:
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'
}