In my play framework (2.5) app, I need to write unit tests for services.
I need to isolate data access logic to able to test service layer in isolation, for this I want to create repository interfaces and MOCK them in my unit tests:
class UserService {
def signUpNewUser(username: String, memberName: String): Future[Unit] {
val userId = 1 // Set to 1 for demo
val user = User(userId, username)
val member = Member(memberName, userId)
// ---- I NEED TO EXECUTE THIS BLOCK WITHIN TRANSACTION ----
for {
userResult <- userRepository.save(user)
memberRepository.save(member)
} yield ()
// ---- END OF TRANSACTION ----
}
}
In the above example, userRepository.save(User)
and memberRepository.save(member)
operations should be performed within transaction.
I don't want to use slick directly in my service layer because it will complicate my tests.
Also, I don't want to use embedded database for my unit tests, elsewhere it would be a NOT unit test, I need complete isolation.
I do not want my repository interfaces to be depended on slick at all, but need something like this:
trait UserRepository {
findById(id: Long): Future[Option[User]]
save(user: User): Future[Unit]
}
how can I achieve this with slick?