Java Spring启动/Spring数据集成测试
未能为集成测试配置Spring引导。冷请看下面的代码: 实体Java Spring启动/Spring数据集成测试,java,spring-boot,spring-data,integration-testing,Java,Spring Boot,Spring Data,Integration Testing,未能为集成测试配置Spring引导。冷请看下面的代码: 实体 @Entity(name = "Item") @Table(name = "item") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Getter @Setter @NoArgsConstructor public class Item extends CoreEntity { @Embedded protected CurrencyAmount
@Entity(name = "Item")
@Table(name = "item")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Getter @Setter @NoArgsConstructor
public class Item extends CoreEntity {
@Embedded
protected CurrencyAmount amount;
@Column(name = "operation_time")
protected ZonedDateTime operationTime;
@Column(name = "description")
protected String description;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "category_id")
protected ItemCategory category;
}
@Entity(name = "Income")
@Table(name = "income")
@Getter @Setter @NoArgsConstructor
public class Income extends Item {
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "budget_id")
private Budget budget;
...
}
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
存储库
@Repository("incomeDao")
public interface IncomeDao extends JpaRepository<Income, Long> {
}
应用程序属性
@Entity(name = "Item")
@Table(name = "item")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Getter @Setter @NoArgsConstructor
public class Item extends CoreEntity {
@Embedded
protected CurrencyAmount amount;
@Column(name = "operation_time")
protected ZonedDateTime operationTime;
@Column(name = "description")
protected String description;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "category_id")
protected ItemCategory category;
}
@Entity(name = "Income")
@Table(name = "income")
@Getter @Setter @NoArgsConstructor
public class Income extends Item {
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "budget_id")
private Budget budget;
...
}
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DaoTestConfiguration.class)
@DataJpaTest
public class IncomeDaoTest {
@Autowired
private IncomeDao incomeDao;
@Test
public void testSave() {
Budget budget = Budget.getBulider().setMonth(Month.NOVEMBER).build();
ItemCategory category = ItemCategory.getBuilder()
.setItemGroup(ItemGroup.INCOME)
.setCathegoryName("Salary")
.build();
Income income = Income.getBuilder()
.setBudget(budget)
.setCurrencyAmount(new CurrencyAmount(Currency.getInstance("USD"), BigDecimal.TEN))
.setDescription("Monthly salary")
.setItemCategory(category)
.setOperationTime(ZonedDateTime.of(2017, 1, 12, 12, 0, 0, 0, ZoneId.of("UTC")))
.build();
incomeDao.save(income);
assertThat(incomeDao.findAll()).containsExactly(income);
}
}
我尝试了一种不同的配置(它的最新版本),但每次都出现相同的异常:
Caused by: org.h2.jdbc.JdbcSQLException: Таблица "INCOME" не найдена
Table "INCOME" not found; SQL statement:
insert into income (model/amount, currency, category_id, description, operation_time, budget_id, id) values (?, ?, ?, ?, ?, ?, ?) [42102-196]
更多关于异常性质的讨论是奇怪的,因为让spring引导根据实体注释自动生成模式的主要思想。因此,在插入时,spring必须创建表,但看起来并没有。如果有人知道我做错了什么,或者有人已经面临这样的问题,请让我知道。谢谢。使用
@springbootest
(满载上下文)或使用@DataJpaTest
(JPA上下文)。从: 可与结合使用的注释 @RunWith(SpringRunner.class)用于典型的JPA测试。可在以下情况下使用: 测试只关注JPA组件 如果您希望加载完整的应用程序配置,但是 使用嵌入式数据库,你应该考虑@ SpringBootTest组合 使用@AutoConfigureTestDatabase而不是此批注 此外,通过将
DaoTestConfiguration
指定为测试类中的配置类:
@SpringBootTest(classes = DaoTestConfiguration.class)
对于作为Spring boot提供的嵌入式数据库,您不依赖默认值
在DaoTestConfiguration
中,您声明了bean:
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(environment.getProperty("spring.datasource.url"));
dataSource.setUsername(environment.getProperty("spring.datasource.username"));
dataSource.setPassword(environment.getProperty("spring.datasource.password"));
return dataSource;
}
要么不创建此数据源,让Spring Boot来创建它,要么在数据源bean声明中指定属性
Spring.jpa.hibernate.ddl auto
。要么使用@springbootest
(满载上下文)要么使用@DataJpaTest
(jpa上下文)。从: 可与结合使用的注释 @RunWith(SpringRunner.class)用于典型的JPA测试。可在以下情况下使用: 测试只关注JPA组件 如果您希望加载完整的应用程序配置,但是 使用嵌入式数据库,你应该考虑@ SpringBootTest组合 使用@AutoConfigureTestDatabase而不是此批注 此外,通过将
DaoTestConfiguration
指定为测试类中的配置类:
@SpringBootTest(classes = DaoTestConfiguration.class)
对于作为Spring boot提供的嵌入式数据库,您不依赖默认值
在DaoTestConfiguration
中,您声明了bean:
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(environment.getProperty("spring.datasource.url"));
dataSource.setUsername(environment.getProperty("spring.datasource.username"));
dataSource.setPassword(environment.getProperty("spring.datasource.password"));
return dataSource;
}
不要创建此数据源,让Spring Boot来创建它,或者在数据源bean声明中指定属性
Spring.jpa.hibernate.ddl auto
。在application.properties中添加此属性
spring.jpa.hibernate.ddl-auto=update
在application.properties中添加此属性
spring.jpa.hibernate.ddl-auto=update
如果你想做@DataJpaTest,我的答案与Bhusan Umiyal类似,但是使用
create
而不是update
spring.jpa.hibernate.ddl-auto=create
正如在座的其他人所说,不要将它与@SpringBootTest
一起使用
还要确保您的测试类与主Spring Boot应用程序类位于同一个或子包中。如果您想执行@DataJpaTest,我的答案与Bhusan Umiyal类似,但使用
create
而不是update
spring.jpa.hibernate.ddl-auto=create
正如在座的其他人所说,不要将它与@SpringBootTest
一起使用
还要确保您的测试类与主Spring Boot应用程序类位于同一个或子包中。
@SpringBootTest
和@DataJpaTest
基本上是互斥的。您要么想要一个完整的测试@SpringBootTest
,要么想要一个切片,但不能两者兼而有之。另外,您正在努力使用@Configuration
类来解决Spring Boot问题,其中没有Spring Boot尚未为您配置的内容。使用框架而不是围绕它工作。谢谢,但它没有成功。我同意spring boot的配置应该很小,因为boot本身代表自动配置,但我在官方文档或谷歌搜索中找到的所有自动配置都不适合。spring boot已经为您完成了配置中的所有工作。。。这没有增加任何东西……同意。根据示例@DataJpaTest应该足够了(考虑到spring boot starter数据jpa、spring boot starter测试和h2在pom中没有版本),但我得到:无法找到@SpringBootConfiguration,您需要使用@ContextConfiguration或@SpringBootTest(类=…)
。问题是否存在于引导依赖项中?如果未找到@SpringBootConfiguration
注释类,则表示无法找到@SpringBootApplication
注释类。这通常是由于奇怪的打包(即从您发布的@Configuration
判断,没有像您这样的共享根包)。@springbootest
和@DataJpaTest
基本上是互斥的。您要么想要一个完整的测试@SpringBootTest
,要么想要一个切片,但不能两者兼而有之。另外,您正在努力使用@Configuration
类来解决Spring Boot问题,其中没有Spring Boot尚未为您配置的内容。使用框架而不是围绕它工作。谢谢,但它没有成功。我同意spring boot的配置应该很小,因为boot本身代表自动配置,但我在官方文档或谷歌搜索中找到的所有自动配置都不适合。spring boot已经为您完成了配置中的所有工作。。。这没有增加任何东西……同意。根据示例@DataJpaTest应该足够了(考虑到spring boot starter数据jpa、spring boot starter测试和h2在pom中没有版本),但我得到:无法找到@SpringBootConfiguration,您需要使用@ContextConfiguration或@SpringBootTes