java为什么@Transactional和@Rollback不起作用?
我将SpringBoot与Kotlin一起使用,我使用的数据库是PostgreSQL。当我编写测试时,我发现@Rollback
不起作用
application-test.properties
# Database configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/magnus
spring.datasource.username=postgres
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.database=POSTGRESQL
IUserInfoRepository.java
interface IUserInfoRepository : JpaRepository<UserInfo, UUID> {
fun findByUserName(name: String): UserInfo?
@Transactional
fun deleteByUserName(name: String): Int
}
UserService.java
@Service
class UserService(@Autowired val userInfoRepository: IUserInfoRepository) {
private lateinit var user: User
fun initService(user: User) {
this.user = user;
}
fun createUser(userName: String, password: ByteArray, nickName: String, isAdmin: Boolean): Boolean {
if (userInfoRepository.findByUserName(userName) != null) {
return false
}
val hashCodeTable = HashCodeTable(RandomStringUtils.randomAscii(16).toByteArray())
val hashedPassword = genHashedPassword(password, hashCodeTable.salt1)
userInfoRepository.save(UserInfo(userName, nickName, hashedPassword, hashCodeTable, isAdmin))
return true
}
fun deleteUser(): Boolean {
return validateUserAndDo {
userInfoRepository.deleteByUserName(user.userName)
true
}
}
fun findUser(): UserInfo? {
return userInfoRepository.findByUserName(user.userName)
}
fun validateUser(): Boolean {
return (findUser()?.hashedPassword?.contentEquals(user.password)) ?: false
}
fun getLoginSalt(userName: String): ByteArray {
return userInfoRepository.findByUserName(userName)?.hashCodeTable?.salt1 ?: ByteArray(0)
}
fun changePassword(newPassword: ByteArray): Boolean {
return validateUserAndDo() {
val oldUser = findUser()!!
val hashedPassword = genHashedPassword(newPassword, oldUser.hashCodeTable.salt1)
val newUser = UserInfo(oldUser.userName, oldUser.nickName, hashedPassword, oldUser.hashCodeTable, oldUser.isAdmin, oldUser.subscriptionTable, oldUser.id, oldUser.categoryTables)
userInfoRepository.save(newUser)
true
}
}
private fun validateUserAndDo(something: () -> Boolean): Boolean {
if (validateUser()) {
return something.invoke()
}
return false
}
fun changeNickName(newNickName: String): Boolean {
return validateUserAndDo() {
val oldUser = findUser()!!
val newUser = UserInfo(oldUser.userName, newNickName, oldUser.hashedPassword, oldUser.hashCodeTable, oldUser.isAdmin, oldUser.subscriptionTable, oldUser.id, oldUser.categoryTables)
userInfoRepository.save(newUser)
true
}
}
fun changePermission(isAdmin: Boolean): Boolean {
return validateUserAndDo() {
val oldUser = findUser()!!
val newUser = UserInfo(oldUser.userName, oldUser.nickName, oldUser.hashedPassword, oldUser.hashCodeTable, isAdmin, oldUser.subscriptionTable, oldUser.id, oldUser.categoryTables)
userInfoRepository.save(newUser)
true
}
}
fun isPermission(): Boolean {
return userInfoRepository.findByUserName(user.userName)?.isAdmin ?: false
}
fun deleteAll(): Boolean {
userInfoRepository.deleteAll()
return true
}
}
UserServiceTest.java
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class UserServiceTest(@Autowired val userService: UserService) {
private lateinit var userName: String
private lateinit var password: ByteArray
@BeforeAll
fun beforeTest() {
userName = "test"
password = "123456".toByteArray()
userService.createUser(userName, password, "Test", false)
val user = User(userName, genHashedPassword(password, userService.getLoginSalt(userName)))
userService.initService(user);
}
@Test
@Rollback
fun `delete user`() {
userService.deleteUser()
Assertions.assertNull(userService.findUser())
}
@Test
@Rollback
fun `get login salt`() {
Assertions.assertEquals(userService.getLoginSalt(userName).contentEquals(ByteArray(0)), false)
}
@Test
@Rollback
fun `validate User`() {
Assertions.assertEquals(userService.validateUser(), true)
}
}
# 1 楼答案
关于@Transactional,Spring文档说明:
关于@Rollback注释,Spring文档说明:
请参阅:https://docs.spring.io/spring/docs/4.2.5.RELEASE/spring-framework-reference/html/integration-testing.html#testcontext-tx