Skip to content

Instantly share code, notes, and snippets.

@daimatz
Last active January 3, 2016 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daimatz/8503238 to your computer and use it in GitHub Desktop.
Save daimatz/8503238 to your computer and use it in GitHub Desktop.
case class User(username: String, password: String)
trait UserRepositoryComponent {
val userRepository: UserRepository
// ordinal class
class UserRepository {
def authenticate(user: User): User = {
println("authenticating user: " + user)
user
}
def create(user: User) = println("creating user: " + user)
def delete(user: User) = println("deleting user: " + user)
}
}
trait UserServiceComponent {
// specify dependencies by self-type annotation
self: UserRepositoryComponent =>
val userService: UserService
class UserService {
def authenticate(username: String, password: String): User =
userRepository.authenticate(new User(username, password))
def create(username: String, password: String) =
userRepository.create(new User(username, password))
def delete(user: User) =
userRepository.delete(user)
}
}
// for Production
trait RealEnvironment
extends UserServiceComponent
with UserRepositoryComponent
{
val userRepository = new UserRepository
val userService = new UserService
}
object Main extends RealEnvironment {
def main(args: Array[String]) {
val user = userService.authenticate("foo", "bar")
println("authenticated user is " + user)
}
}
// for Test
import org.mockito._
import org.specs2.mutable._
trait TestEnvironment
extends UserServiceComponent
with UserRepositoryComponent
{
// all dependant objects are mocked
val userRepository = Mockito.mock(classOf[UserRepository])
val userService = Mockito.mock(classOf[UserService])
}
class UserServiceSpec extends Specification with TestEnvironment {
// create a non-Mock object that you want to test
override val userService = new UserService
"authenticate" should {
"return user" in {
val user = User("test", "test")
// note that denpendant object `userRepository` is mocked
Mockito.when(userRepository.authenticate(user)).thenReturn(user)
userService.authenticate("test", "test") must_== user
}
}
}
@daimatz
Copy link
Author

daimatz commented Jan 19, 2014

$ scalac -cp mockito-all-1.9.5.jar:specs2_2.10-2.3.7.jar:scalaz-concurrent_2.10-7.0.4.jar:scalaz-core_2.10-7.0.0.jar CakePattern.scala
$ scala Main
authenticating user: User(foo,bar)
authenticated user is User(foo,bar)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment