java - 如何配置 Spring Boot、Spring JPA、Spring Test 和 Hibernate 来创建、使用和删除给定的 PostgreSQL 架构?

标签 java hibernate postgresql spring-boot spring-data-jpa

如何配置 Spring BootSpring Data JPASpring TestHibernate 来为保存和检索对象的单元测试创​​建、使用和删除给定的 PostgreSQL 架构?

在测试开始之前,Spring Test 应该为测试创建数据库模式。每个测试方法应该在单个事务中运行,完成后,测试方法应该回滚所有数据库操作。在所有测试方法结束时,测试应删除架构。

在目前的形式中,AccountRepositoryTest 通过了,但在架构 public 中创建表 account,而不是创建表 account > 在新模式 springboot 中。

配置:

  • Spring Boot 1.3.0.BUILD-SNAPSHOT
  • Spring Data JPA 1.9.0.RELEASE
  • Spring 测试 4.2.3.BUILD-SNAPSHOT
  • Hibernate 4.3.11.Final
  • PostgreSQL 9.5.4

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

public interface AccountRepository extends CrudRepository<Account, UUID> {
    Account findByNumber(String number);
    Account findByOwner(String owner);
}

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

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

最佳答案

您最好的选择是使用 spring.jpa.hibernate.ddl-auto Spring Boot 属性。将其包含在application-test.properties中(或 application-test.yaml ,视情况而定)并将其值设置为 create-drop (例如 spring.jpa.hibernate.ddl-auto=create-drop 。更多详细信息请参阅 Spring Boot documentation

但是,我建议使用内存数据库来运行测试,因为您不会冒将测试指向实际数据库模式的风险,测试将运行得更快,并且您将能够运行持续集成构建在 Travis-CI 或 Shippable 等第三方系统上。

关于java - 如何配置 Spring Boot、Spring JPA、Spring Test 和 Hibernate 来创建、使用和删除给定的 PostgreSQL 架构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442314/

相关文章:

java - 将 ArrayList 内容打印到 TextArea 中

java - 在 Linux 下运行时出现奇怪的 hibernate 异常

PostgreSQL 序列连接到列

c++ - PostgreSQL C++ libpq编码UTF-8问题

java - 尝试以编程方式将 GLSurfaceView 添加到布局

java - 调用 Thread.isInterrupted() 的性能成本是多少?

java - 如何使用 Spring 和 Hibernate 调用具有两个参数的存储过程

java - 使用 spring 更新单个对象并通过 url 发送

postgresql - psycopg2execute_values 失败,supabase 中的行数超过 100 行?

java testng 重试逻辑