未能配置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 boot根据实体注释自动生成模式。因此,在插入的时候,spring必须创建表,但是看起来它并没有创建。如果有人给我一个想法,我做错了什么,或者如果有人已经面临这样的问题-请让我知道。谢了。
如果你想做一个@DataJpaTest,我的答案与Bhusan Umiyal相似,但使用创建
而不是update
spring.jpa.hibernate.ddl-auto=create
就像这里的其他人说的那样,不要和
@SpringBootTest
一起使用它。
还要确保您的测试类与主要Spring Boot应用程序类在同一个或子包中。
在application.properties中添加此属性
spring.jpa.hibernate.ddl-auto=update
使用< code>@SpringBootTest(完全加载的上下文)或使用< code>@DataJpaTest (JPA上下文)。< br >
从留档:
可以与@RunWith(SpringRunner.class)结合使用的注释,用于典型的JPA测试。当测试仅关注 JPA 组件时可以使用。
...
如果您希望加载完整的应用程序配置,但使用嵌入式数据库,您应该考虑将@SpringBoottest与@AutoCon
此外,通过将 DaoTestConfiguration
指定为测试类中的配置类:
@SpringBootTest(classes = DaoTestConfiguration.class)
你不依赖于嵌入式数据库提供的默认值 Spring 引导,就像在 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声明中指定属性< code > spring . JPA . hibernate . DDL-auto 。
我试图为一个Spring引导项目写一个集成测试。不幸的是,我对实现感到困惑。 下面是已尝试的示例代码段 问题 我是否需要一个单独的,带有注释以支持集成测试
我正在测试我的spring boot应用程序。我正在做集成测试,但由于url返回的响应无法jsonfied,测试一直失败。请帮忙。以下是测试功能: 错误:
我有一个Spring Boot集成测试,其类定义如下。 我得到以下例外: 我的TestApplication类定义如下: properties-context.xml定义如下: 这从一个名为external.properties.的文件中读取,在我的外部属性文件中设置了属性'spring.main.allow-Bean-定义-覆盖=true'。我添加此项是因为当我运行应用程序时,出现了相同的异常,
我试图使用Spring Data连接Elasticsearch 6.1(我使用Spring Boot) 但是提到最新版本只支持5.2。,有没有支持ES 6.1的替代方案? https://github.com/spring-projects/spring-data-elasticsearch/blob/master/README.md
我需要从远程SFTP服务器下载一个文件,并使用spring batch处理它们。我已经实现了使用Spring集成下载文件的代码。但我无法从Spring集成组件启动Spring批处理作业。我有以下代码: 但这不起作用(上一个方法中的错误),因为找不到文件类型的bean。我不能把这两部分连在一起。如何连接集成和批处理?
主要内容:1. 项目依赖,2. Spring组件,3. TestNG + Spring在本教程中,我们将演示如何使用TestNG测试Spring的组件。 使用的工具 : TestNG 6.8.7 Spring 3.2.2.RELEASE Maven 3 Eclipse IDE 1. 项目依赖 为了演示,首先创建一个名称为:TestngSpringIntegration 的 Maven 项目。 要将Spring与TestNG集成,您需要包依懒,添加以下内容: 创建文件:pom.xml