使用inmemory数据库进行Spring启动测试
我的问题是,我无法创建使用内存数据库的测试。所有时间都有错误,例如: 原因:java.lang.IllegalStateException:无法检索@EnableAutoConfiguration基本包 如何使用H2 inmemory数据库进行测试 实体类:使用inmemory数据库进行Spring启动测试,spring,spring-boot,spring-data,spring-data-jpa,Spring,Spring Boot,Spring Data,Spring Data Jpa,我的问题是,我无法创建使用内存数据库的测试。所有时间都有错误,例如: 原因:java.lang.IllegalStateException:无法检索@EnableAutoConfiguration基本包 如何使用H2 inmemory数据库进行测试 实体类: @Entity @Table(name = "names") public class Names{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@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();
}
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(){
}
}
渐变依赖项:
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'
}
请将@AutoConfigurationPackage添加到@SpringBootConfiguration类中,该类是必需的,因为示例中没有@SpringBootApplication类:
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigurationPackage
@SpringBootConfiguration
public class NamesTest {
@Autowired
private NamesRepository names;
@Test
public void firsTest(){
}
}
请将@AutoConfigurationPackage添加到@SpringBootConfiguration类中,该类是必需的,因为示例中没有@SpringBootApplication类:
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigurationPackage
@SpringBootConfiguration
public class NamesTest {
@Autowired
private NamesRepository names;
@Test
public void firsTest(){
}
}
更改后:java.lang.IllegalStateException:未能加载ApplicationContext->>原因:java.lang.IllegalArgumentException:必须至少存在一个JPA元模型!删除依赖项spring启动测试禁用JpaRepositoriesAutoConfiguration@EnableAutoConfiguration(排除={JpaRepositoriesAutoConfiguration.class})更改后:java.lang.IllegalStateException:未能加载ApplicationContext->>原因:java.lang.IllegalArgumentException:必须至少存在一个JPA元模型!删除依赖项spring启动测试禁用JpaRepositoriesAutoConfiguration@EnableAutoConfiguration(排除={JpaRepositoriesAutoConfiguration.class})