使用内存数据库进行Spring启动测试

时间:2018-05-23 15:20:41

标签: spring spring-boot spring-data spring-data-jpa

我的问题是我无法创建在内存数据库中使用的测试。所有时间都有错误,例如:

引起: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'
}

1 个答案:

答案 0 :(得分:1)

请将 @AutoConfigurationPackage 添加到 @SpringBootConfiguration 类,这是必需的,因为示例中没有 @SpringBootApplication 类:

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigurationPackage
@SpringBootConfiguration
public class NamesTest {

    @Autowired
    private NamesRepository names;

    @Test
    public void firsTest(){

    }
}