我正在使用Spring Boot 1.4.0.M3。當@Commit使用@Transactional時,無法測試預期的異常
我有,有一個username
這應該是唯一的@Entity
:
@NotNull
@Column(unique = true)
@Pattern(regexp = "[a-zA-Z_\\-\\.0-9]+")
@Size(min = 1, max = 30)
private String username;
使用彈簧數據儲存庫,我想測試是否會有一個異常時,使用重複的用戶名。這個測試工作:
@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.springframew[email protected]589b3632] to process 'after' execution for test: method [public void com.spring.boot.test.user.UserRepositoryIntegrationTest.test()], instance [[email protected]], exception [java.lang.AssertionError: Expected exception: org.springframework.dao.DataIntegrityViolationException]
爲什麼測試失敗了嗎?難道JUnit會在Spring提交事務之前檢查異常是否被拋出?
這是預期... AFTER事務提交會發生的異常。該方法結束後,事務提交。所以在檢查時基本上沒有例外。你用這個改變了測試的行爲。你需要在你的方法中提交。 –
「允許TestExecutionListener引發異常」說了這一切。你想提交但你想斷言提交失敗。 JUnit和Spring沒有意識到對方,所以你不能使用JUnit斷言來處理那些東西。正如Martin所說的,在測試中使用'TestTransaction#end'。 –
感謝您指點該班。不知道它存在。我已經添加了完整的工作版本的答案。 –