Created
October 16, 2012 19:11
-
-
Save eboto/3901317 to your computer and use it in GitHub Desktop.
Examples of writing testable code using traits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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