Integrates ScalaTest with Spring to manage test context lifecycle using standard Spring annotations and a stackable Scala trait.
libraryDependencies ++= "com.github.scalaspring" %% "scalatest-spring" % "0.3.1"- Extend the TestContextManagement trait in your test to automatically set up and tear down your Spring test context.
- Use the standard Spring ContextConfiguration annotation (see Spring Testing Annotations) to identify the configuration to use.
@ContextConfiguration(classes = Array(classOf[SimpleConfiguration]))
class SimpleTestSpec extends FlatSpec with TestContextManagement with Matchers {
// Use Spring compatible annotations (@Autowired or @Inject) to inject necessary dependencies
// Note that Spring will inject val (read-only) fields, so this field will be non-null when tests run
@Autowired val injected: Seq[String] = null
"Dependency" should "be injected" in {
// Some test that uses the injected dependency
injected shouldEqual Seq("foo")
}
}
@Configuration
class SimpleConfiguration {
@Bean
def someSeq: Seq[String] = Seq("foo")
}- The
TestContextManagementclass is implemented as a stackable trait extending theBeforeAndAfterAllScalaTest trait. - Spring's
TestContextManagerclass is used under the hood which reads the@ContextConfigurationattribute to set up the appropriate test context. - If you need your own
beforeAllorafterAlllogic, be sure to callsuper.beforeAllandsuper.afterAllin your implementation to ensure that contexts are properly created and destroyed. See the project test source for an example.