2011-06-08 9 views
13

मैं एक स्प्रिंग संदर्भ से @Autowired क्षेत्रों, लेकिन सबसे Scalatest परीक्षण के साथ मेरी ScalaTest परीक्षण से पॉप्युलेट करने के साथ ScalaTest एकीकृत करना (जैसे FeatureSpec द्वारा SpringJUnit4ClassRunner.class नहीं चलाया जा सकता जावा, लेकिन आप सार) मिलता है।मैं कैसे स्प्रिंग

मैं कैसे ScalaTest के लिए एक ApplicationContext से @Autowired खानों को भरने करते हैं?

class AdminLoginFeatureTest extends FeatureSpec with GivenWhenThen with ShouldMatchersForJUnit { 

    @Autowired val app: WebApplication = null 
    @Autowired val siteDAO: SiteDAO = null 

    feature("Admin Login") { 
    scenario("Correct username and password") {...} 

उत्तर

26

TestContextManager का उपयोग करें, क्योंकि यह संदर्भों को कैश करता है ताकि उन्हें प्रत्येक परीक्षण का पुनर्निर्माण नहीं किया जा सके। यह कक्षा एनोटेशन से कॉन्फ़िगर किया गया है।

@ContextConfiguration(
    locations = Array("myPackage.UnitTestSpringConfiguration"), 
    loader = classOf[AnnotationConfigContextLoader]) 
class AdminLoginFeatureTest extends FeatureSpec with GivenWhenThen with ShouldMatchers { 

    @Autowired val app: WebApplication = null 
    @Autowired val siteDAO: SiteDAO = null 
    new TestContextManager(this.getClass()).prepareTestInstance(this) 

    feature("Admin Login") { 
    scenario("Correct username and password") {...} 
    } 
} 
3

मैं वसंत 4 + स्काला 2.11 के साथ डंकन जवाब उपयोग करने की कोशिश और मैं निम्नलिखित त्रुटि मिली:

java.lang.IllegalStateException: Test class [TestGateway] has been configured with @ContextConfiguration's 'locations' (or 'value') attribute {GatewayContextConfiguration}, but AnnotationConfigContextLoader does not support resource locations. 

मैं जब एक स्ट्रिंग के बजाय ContextConfiguration को विन्यस्त एक वर्ग का उपयोग करने के लिए अपने कोड में और सुधार करना पड़ा :

@ContextConfiguration(classes = Array(classOf[GatewayContextConfiguration]), 
         loader = classOf[AnnotationConfigContextLoader]) 
class TestGateway extends FlatSpec with Matchers { 

    @Autowired val gpClient: IGlobalPropsWSClient = null 

    new TestContextManager(this.getClass()).prepareTestInstance(this) 

    "Echo" should "return what it was sent." in { 
    val gateway = new CasaWsGateway 
    gateway.echo("This is a test") should be ("This is a test") 
    } 
} 
3

यहाँ (है, ताकि आप अपने खुद के 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) 
    } 
} 
0

आप स्प्रिंग बूट आप TestContextManager इस्तेमाल कर सकते हैं (अन्य टिप्पणी सुझाव के रूप में) का उपयोग कर रहे हैं, तो और @SpringBootTest एनोटेशन।

@RunWith(classOf[SpringRunner]) 
@SpringBootTest(webEnvironment = RANDOM_PORT) 
class CustomerControllerIT extends FeatureSpec with GivenWhenThen with Matchers { 

    @Autowired 
    var testRestTemplate: TestRestTemplate = _ 
    new TestContextManager(this.getClass).prepareTestInstance(this) 

    @LocalServerPort 
    val randomServerPort: Integer = null 

    val baseUrl = s"http://localhost:$randomServerPort" 

    feature("Customer controller") { 

    scenario("Find customer by id") { 

     Given("a customer id") 
     val id = 1 

     When("a request to /customers/{id} is sent") 
     val url = s"$baseUrl/customers/$id" 
     val response = testRestTemplate.getForEntity(url, classOf[Customer]) 

     Then("we get a response with the customer in the body") 
     response.getBody.getId shouldBe 1 
     response.getBody.getName shouldBe "Bob" 

    } 

    } 

} 

यहाँ कैसे वसंत बूट और ScalaTest के साथ एकीकरण के परीक्षण और इकाई परीक्षण करने के बारे में एक पोस्ट है: ignaciosuay.com/testing-spring-boot-with-scalatest/

यह मैं कैसे scalaTest और वसंत बूट के साथ एक नियंत्रक का परीक्षण है

संबंधित मुद्दे