Skip to content

Instantly share code, notes, and snippets.

@eboto
Created October 16, 2012 19:11
Show Gist options
  • Save eboto/3901317 to your computer and use it in GitHub Desktop.
Save eboto/3901317 to your computer and use it in GitHub Desktop.
Examples of writing testable code using traits
trait MyService {
def metricProvider: OtherService1
// My work goes below
def getMetrics: Seq[Metric] {
// code that actually uses dep1 and dep2 to get metrics
}
}
object MyService extends MyService {
override val metricProvider = new CloudWatchService
}
class MyServiceTests extends org.scalatest.FlatSpec with org.scalatest.matchers.ShouldMatchers {
"MyService" should "return no metrics if it ghets none from its metric provider" in {
// Set up
val underTest = new MyService {
override val metricProvider = mock[CloudWatchService]
}
underTest.metricProvider.getCloudwatchMetrics returns List.empty[Metric]
// Run test and check expectations
underTest.getMetrics should be (Seq.empty[Metric])
}
}
class Person {
public String getName()
}
object Person {
public Array<Person> getPeople();
}
Person.getName() // doesn't compile
new Person().getPeople // doesn't compile
class MyServiceImpl @Inject(val dep1: OtherService1, val dep2: OtherService2) extends MyService
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment