यहाँ (है, ताकि आप अपने खुद के beforeAll()
और afterAll()
तरीकों हो सकता है यदि आवश्यक हो तो) एक संस्करण एक stackable विशेषता के रूप में लागू है जो संदर्भ जीवन चक्र को पूरा करने के लिए TestContextManager
का उपयोग करता है।
मैंने अन्य पदों में सुझाए गए कच्चे TestContextManager.prepareTestInstance()
समाधान की कोशिश की, लेकिन ध्यान दिया कि मेरे संदर्भ बंद नहीं हो रहे थे जिसके परिणामस्वरूप एसबीटी कंसोल का उपयोग करते समय प्रत्येक लगातार परीक्षण चलाने के बाद साइड इफेक्ट्स और कचरा जमा हो जाता है।
@ContextConfiguration(classes = Array(classOf[SomeConfiguration]))
class SomeTestSpec extends FlatSpec with TestContextManagement {
// Use standard Autowired Spring annotation to inject necessary dependencies
// Note that Spring will inject val (read-only) fields
@Autowired
val someDependency: SomeClass = null
"Some test" should "verify something" in {
// Test implementation that uses injected dependency
}
}
TestContextManagement Gist
import org.scalatest.{BeforeAndAfterAll, Suite}
import org.springframework.core.annotation.{AnnotatedElementUtils, AnnotationAttributes}
import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.context.{TestContext, TestContextManager}
import org.springframework.test.context.support.DirtiesContextTestExecutionListener
import org.springframework.util.Assert
/**
* Manages Spring test contexts via a TestContextManager.
*
* Implemented as a stackable trait that uses beforeAll() and afterAll() hooks to invoke initialization
* and destruction logic, respectively.
* Test contexts are marked dirty, and hence cleaned up, after all test methods have executed.
* There is currently no support for indicating that a test method dirties a context.
*
* @see org.springframework.test.context.TestContextManager
*/
trait TestContextManagement extends BeforeAndAfterAll { this: Suite =>
private val testContextManager: TestContextManager = new TestContextManager(this.getClass)
abstract override def beforeAll(): Unit = {
super.beforeAll
testContextManager.registerTestExecutionListeners(AlwaysDirtiesContextTestExecutionListener)
testContextManager.beforeTestClass
testContextManager.prepareTestInstance(this)
}
abstract override def afterAll(): Unit = {
testContextManager.afterTestClass
super.afterAll
}
}
/**
* Test execution listener that always dirties the context to ensure that contexts get cleaned after test execution.
*
* Note that this class dirties the context after all test methods have run.
*/
protected object AlwaysDirtiesContextTestExecutionListener extends DirtiesContextTestExecutionListener {
@throws(classOf[Exception])
override def afterTestClass(testContext: TestContext) {
val testClass: Class[_] = testContext.getTestClass
Assert.notNull(testClass, "The test class of the supplied TestContext must not be null")
val annotationType: String = classOf[DirtiesContext].getName
val annAttrs: AnnotationAttributes = AnnotatedElementUtils.getAnnotationAttributes(testClass, annotationType)
val hierarchyMode: DirtiesContext.HierarchyMode = if ((annAttrs == null)) null else annAttrs.getEnum[DirtiesContext.HierarchyMode]("hierarchyMode")
dirtyContext(testContext, hierarchyMode)
}
}
स्रोत
2015-03-22 02:26:08