[英]How can I configure Spring Boot, Spring JPA, Spring Test, and Hibernate to create, use, and drop a given PostgreSQL schema?
How can I configure Spring Boot , Spring Data JPA , Spring Test , and Hibernate to create, use, and drop a given PostgreSQL schema for a unit test which saves and retrieves an object? 如何配置Spring Boot , Spring Data JPA , Spring Test和Hibernate来创建,使用和删除给定的PostgreSQL模式进行单元测试,以保存和检索对象?
Before the test starts Spring Test should create the database schema for the test. 在测试开始之前,Spring Test应该为测试创建数据库模式。 Each test method should run within a single transaction and after it finishes, the test method should roll back all database operations.
每个测试方法应在单个事务中运行,完成后,该测试方法应回滚所有数据库操作。 At the end of all test methods, the test should drop the schema.
在所有测试方法的最后,测试应删除架构。
In its present form, AccountRepositoryTest
passes, but creates table account
in schema public
instead of creating table account
in new schema springboot
. 在目前的形式,
AccountRepositoryTest
通过,但创建表account
架构中public
,而不是创建表account
在新的模式springboot
。
Configuration: 组态:
AccountRepositoryTest.java AccountRepositoryTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class AccountRepositoryTest {
final static Logger logger = LoggerFactory.getLogger(AccountRepositoryTest.class);
@Autowired
AccountRepository accountRepository;
@Test
public void testSaveAccount() {
final Account newAccount = accountRepository.save(new Account("123", "Derek Mahar", 500.00));
final Account readAccount = accountRepository.findOne(newAccount.getId());
logger.info("New account UUID={}", newAccount.getId());
assertEquals(newAccount.getBalance(), readAccount.getBalance(), 0.001);
assertEquals(newAccount.getNumber(), readAccount.getNumber());
assertEquals(newAccount.getOwner(), readAccount.getOwner());
}
@Test
public void testFindByNumber() {
final Account newAccount = accountRepository.save(new Account("456", "Steve Balmer", 500.00));
final Account readAccount = accountRepository.findByNumber(newAccount.getNumber());
logger.info("New account UUID={}", newAccount.getId());
assertEquals(newAccount.getBalance(), readAccount.getBalance(), 0.001);
assertEquals(newAccount.getNumber(), readAccount.getNumber());
assertEquals(newAccount.getOwner(), readAccount.getOwner());
}
@Test
public void testFindByOwner() {
final Account newAccount = accountRepository.save(new Account("789", "Bill Gates", 500.00));
final Account readAccount = accountRepository.findByNumber(newAccount.getNumber());
logger.info("New account UUID={}", newAccount.getId());
assertEquals(newAccount.getBalance(), readAccount.getBalance(), 0.001);
assertEquals(newAccount.getNumber(), readAccount.getNumber());
assertEquals(newAccount.getOwner(), readAccount.getOwner());
}
}
AccountRepository.java AccountRepository.java
public interface AccountRepository extends CrudRepository<Account, UUID> {
Account findByNumber(String number);
Account findByOwner(String owner);
}
Account.java Account.java
@Entity
public class Account implements Serializable {
@Column(nullable = false)
private double balance;
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column
private UUID id;
@Column(nullable = false)
private String number;
@Column(nullable = false)
private String owner;
public Account(String number, String owner, double balance) {
this.number = number;
this.owner = owner;
this.balance = balance;
}
public Account() {
}
public double getBalance() {
return balance;
}
public UUID getId() {
return id;
}
public String getNumber() {
return number;
}
public String getOwner() {
return owner;
}
}
application.properties application.properties
spring.datasource.url=jdbc:postgresql:springboot
spring.datasource.username=springboot
spring.datasource.password=springboot
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.schema=springboot
spring.datasource.initialize=true
spring.jpa.database=springboot
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
Your best bet would be to use the spring.jpa.hibernate.ddl-auto
Spring Boot property. 最好的选择是使用
spring.jpa.hibernate.ddl-auto
Spring Boot属性。 Include it in application-test.properties
(or application-test.yaml
, as the case may be) and set its value to create-drop
(for example, spring.jpa.hibernate.ddl-auto=create-drop
. More details are available in the Spring Boot documentation . 将其包含在
application-test.properties
(或视情况而定, application-test.yaml
)中,并将其值设置为create-drop
(例如, spring.jpa.hibernate.ddl-auto=create-drop
。更多详细信息在Spring Boot文档中可用。
However, I would recommend using an in-memory database for running the tests as you will not run the risk of pointing the tests to an actual database schema, the tests will run faster and you will be able to run continuous integration builds on third-party systems like Travis-CI or Shippable. 但是,我建议您使用内存数据库来运行测试,因为这样就不会冒将测试指向实际数据库架构的风险,测试运行速度更快,并且您将能够在第三个数据库上运行持续集成构建。第三方系统,例如Travis-CI或Shippable。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.