gpt4 book ai didi

java - 将@Transactional 与@Commit 一起使用时无法测试预期异常

转载 作者:行者123 更新时间:2023-12-01 14:06:15 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 1.4.0.M3。

我有一个 @Entity,它的 username 应该是唯一的:

@NotNull
@Column(unique = true)
@Pattern(regexp = "[a-zA-Z_\\-\\.0-9]+")
@Size(min = 1, max = 30)
private String username;

使用 Spring Data Repository,我想测试使用重复用户名时是否会出现异常。该测试有效:
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserRepositoryIntegrationTest {

@Autowired
private UserRepository repository;

@Test(expected = DataIntegrityViolationException.class)
public void test() {
repository.save(User.createAdmin(repository.nextId(), "wim", "123456"));
repository.save(User.createAdmin(repository.nextId(), "wim", "123456"));
}
}

但是,当使用 @Transactional 添加 @Commit 时,此测试失败:
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
public class UserRepositoryIntegrationTest {

@Autowired
private UserRepository repository;

@Test(expected = DataIntegrityViolationException.class)
@Commit
public void test() {
repository.save(User.createAdmin(repository.nextId(), "wim", "123456"));
repository.save(User.createAdmin(repository.nextId(), "wim", "123456"));
}
}

但是查看日志记录,正在抛出 DataIntegrityViolationException:

2016-05-24 09:05:16.619 ERROR 22790 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Unique index or primary key violation: "UK_D8HGQ87BS4VPMC81NQ9G69G8X_INDEX_D ON PUBLIC.MYPROJECT_USER(USERNAME) VALUES ('wim', 1)"; SQL statement: insert into myproject_user (password, username, role, id) values (?, ?, 'ADMIN', ?) [23505-191] 2016-05-24 09:05:16.620 INFO 22790 --- [
main] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements 2016-05-24 09:05:16.629 WARN 22790 --- [ main] o.s.test.context.TestContextManager
: Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@589b3632] to process 'after' execution for test: method [public void com.spring.boot.test.user.UserRepositoryIntegrationTest.test()], instance [com.spring.boot.test.user.UserRepositoryIntegrationTest@3ca278bc], exception [java.lang.AssertionError: Expected exception: org.springframework.dao.DataIntegrityViolationException]



为什么测试失败?会不会是 JUnit 在 Spring 提交事务之前检查是否抛出了异常?

最佳答案

你不需要 promise 。您只需要将工作单元刷新到数据库即可查看 DataIntegrityViolationException

这在 Spring Reference Manual 中关于“误报”的注释中有所描述。但是请注意,EntityManager 必须通过 @PersistenceContext (而不是 @Autowired )注入(inject)。

以下应该适合您:

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class UserRepositoryIntegrationTest {

@Autowired
UserRepository repository;

@PersistenceContext
EntityManager entityManager;

@Test(expected = DataIntegrityViolationException.class)
public void test() {
repository.save(User.createAdmin(repository.nextId(), "wim", "123456"));
repository.save(User.createAdmin(repository.nextId(), "wim", "123456"));

entityManager.flush();
}
}

问候,

Sam(Spring TestContext 框架的作者)

关于java - 将@Transactional 与@Commit 一起使用时无法测试预期异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37406714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com