[英]Spring Boot / Spring Data integration tests
没有配置Spring Boot进行集成测试。 太冷了,请看下面的代码:
实体
@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;
...
}
资料库
@Repository("incomeDao")
public interface IncomeDao extends JpaRepository<Income, Long> {
}
测试配置
@Configuration
@EnableJpaRepositories(basePackages = "dao.item")
@EntityScan(basePackages = {"model"})
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class DaoTestConfiguration {
@Autowired
private Environment environment;
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.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必须创建表,但看起来并没有。 如果有人告诉我我做错了什么,或者如果有人已经遇到这样的问题,请让我知道。 谢谢。
使用@SpringBootTest
(完全加载的上下文)或使用@DataJpaTest
(JPA上下文)。
从文档中 :
可以与@RunWith(SpringRunner.class)结合使用的批注,用于典型的JPA测试。 当测试仅针对JPA组件时可以使用。
...
如果要加载完整的应用程序配置,但使用嵌入式数据库,则应考虑将@SpringBootTest与@AutoConfigureTestDatabase结合使用,而不要使用此注释。
此外,通过在测试类DaoTestConfiguration
指定为配置类:
@SpringBootTest(classes = DaoTestConfiguration.class)
您不依赖于DaoTestConfiguration
提供的Spring boot提供的嵌入式数据库的默认值,而是声明了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
如果您想执行@DataJpaTest,我的答案类似于Bhusan Umiyal,但使用create
而不是update
。
spring.jpa.hibernate.ddl-auto=create
就像这里的每个人都说过的那样,不要将其与@SpringBootTest
一起使用。
还要确保测试类与主Spring Boot应用程序类位于相同或子包中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.