2011-04-22 8 views
11

मुझे स्कैला में लिखे गए कुछ जुनीट परीक्षणों को विरासत में मिला है जिन्हें @BeforeClass semantics का उपयोग करने के लिए ठीक करने की आवश्यकता है। मैं समझता हूं कि @BeforeClass एनोटेशन केवल स्थिर विधियों पर लागू होना चाहिए। मैं समझता हूं कि "साथी" वस्तुओं (स्केल कक्षाओं के विपरीत) में परिभाषित विधियां स्थैतिक हैं। टेस्ट क्लास में व्यक्तिगत इंस्टेंस विधियों से पहले एक बार कॉल करने के लिए मुझे टेस्ट विधि कैसे मिल सकती है?स्कैला में लिखे गए जुनीट 4 टेस्ट में @BeforeClass अर्थशास्त्र को आप कैसे कार्यान्वित करते हैं?

उत्तर

0

आप निर्दिष्ट नहीं करते हैं कि क्या आप ओओ प्रोग्रामिंग भावना में विरासत में हैं या "किसी और से लिया गया" शब्द की भावना है।

उत्तरार्द्ध मामले में, मैं तुम्हें सलाह देते हैं कि या तो ScalaTest या Specs2 का उपयोग कर बात को फिर से लिखने और एक JUnit परीक्षण के रूप में बेनकाब (दोनों चौखटे इस समर्थन) इतना है कि यह जो कुछ भी अन्य उपकरणों और प्रक्रियाओं के साथ एकीकृत करने के लिए करेंगे आप पहले से ही जगह में है।

+0

मैं मतलब "प्राप्त" मानव अर्थों में। आज मैं स्कैलाटेस्ट पर पढ़ रहा हूं और शायद उस दिशा में आगे बढ़ूंगा। धन्यवाद। –

16
object TestClass { 

    @BeforeClass 
    def stuff() { 
    // beforeclass stuff 
    } 

} 

class TestClass { 

    @Test 
    ... 

} 

काम करने के लिए लगता है ...

2

मैं स्काला के साथ इस सुविधा को लागू करने के लिए specs2 को स्थानांतरित करने के लिए किया था। मूल समस्या वाले लोगों की मदद करने के लिए बस एक उदाहरण जोड़ना जो अभी तक specs2 नहीं जानता है।

specs2 तरीका टेस्ट सूट सेट-अप और आंसू-डाउन को पूरा करने के लिए "चरण" की अवधारणा का उपयोग करता है। यदि आप जुनीट्रुनर के साथ भागते हैं तो आपकी सभी चींटी स्क्रिप्ट और आईडीई जो जुनीट का उपयोग करती हैं, उन्हें अभी भी पता चल जाएगा कि इसे कैसे चलाया जाए। यहाँ specs2 से एक परिवर्तनशील विनिर्देश का उपयोग कर एक उदाहरण है:

import org.specs2.mutable.Specification 
import org.junit.runner.RunWith 
import org.specs2.runner.JUnitRunner 

@RunWith(classOf[JUnitRunner]) 
class MutableSpecs2ExampleTest extends Specification { 

    var firstStep: String = null 
    var secondStep: String = null 
    var thirdStep: String = null 
    //Steps are guaranteed to run serially 
    step{println("Loading Spring application context...");firstStep="Hello World"} 
    step{println("Setting up mocks...");secondStep = "Hello Scala"} 

    //The fragments should be run in parallel by specs2 
    "Some component Foo in my project" should{ 
    " pass these tests" in { 
     println("Excersizing some code in Foo") 
     firstStep must startWith("Hello") and endWith("World") 
    } 
    " pass theses other tests" in { 
     println("Excersizing some other code in Foo") 
     firstStep must have size(11) 
    } 
    } 
    "Some component Bar in my project" should{ 
    " give the correct answer" in { 
     println("Bar is thinking...") 
     secondStep must startWith("Hello") and endWith("Scala") 
     thirdStep must be equalTo null 
    } 
    } 
    step{println("Tearing down resources after tests...");thirdStep = "Hello Specs2"} 
} 

और यहाँ एक गैर परिवर्तनशील विनिर्देशन के साथ एक उदाहरण है:

import org.specs2.Specification 
import org.specs2.specification.Step 
import org.junit.runner.RunWith 
import org.specs2.runner.JUnitRunner 

@RunWith(classOf[JUnitRunner]) 
class Specs2ExampleTest extends Specification{ 
    var firstStep: String = null 
    var secondStep: String = null 
    var thirdStep: String = null 

    def is = 
    "This is a test with some set-up and tear-down examples"^
                  p^ 
    "Initialize"           ^
                  Step(initializeDependencies())^ 
                  Step(createTestData())^ 
    "Component Foo should"        ^
     "perform some calculation X "      !testX^ 
     "perform some calculation Y"       !testY^ 
                  p^ 
    "Tidy up"           ^
                  Step(removeTestData())^ 
                  end 
    def testX = { 
     println("testing Foo.X") 
     firstStep must be equalTo("Hello World") 
    } 
    def testY = { 
     println("testing Foo.Y") 
     secondStep must be equalTo("Hello Scala") 
     thirdStep must be equalTo null 
    } 

    def initializeDependencies(){ 
    println("Initializing Spring applicaiton context...") 
    firstStep = "Hello World" 
    } 
    def createTestData(){ 
    println("Inserting test data into the db...") 
    secondStep = "Hello Scala" 
    } 

    def removeTestData(){ 
    println("Removing test data from the db...") 
    println("Tearing down resources...") 
    thirdStep = "Hello Specs2" 
    } 
} 
संबंधित मुद्दे

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