2010-06-03 4 views
13

मैंने देखा है कि कुछ लोग इसे करने के लिए मेवेन-एसक्यूएल-प्लगइन का उपयोग करते हैं। लेकिन ऐसा लगता है कि यह एक ऐसा काम है जो डीबीयूनीट के लिए बेहतर है .... शायद पूरे टेस्ट सूट की शुरुआत में।मैवेन/जुनीट/डीबीयूनीट प्रोजेक्ट पर एकीकरण परीक्षण से पहले/बाद में डेटाबेस बनाने/ड्रॉप करने का सबसे अच्छा तरीका?

यहां सबसे अच्छा अभ्यास क्या है?

उत्तर

14

मैं Maven SQL Plugin

इसका उपयोग और सुनिश्चित करें कि आप बनाते हैं और अपने परीक्षण से पहले पॉप्युलेट और फिर अपने परीक्षणों के बाद छोड़ बनाने बंद काफी बेहतर रहे हैं का उपयोग करें। तुम भी उपयोग करने के लिए बना सकते हैं या, या ड्रॉप की जगह अगर अपनी रचना लिपि में मौजूद है चाहता हूँ घटना है कि एक परीक्षण में विफल रहता है और कुछ असंगत स्थिति में डेटाबेस पत्ते में (अपने डेटाबेस संभालने इसका समर्थन करता है)।

+0

क्या आप डीबी असंगत स्थिति का उदाहरण दे सकते हैं? इसे चलाने के अगले परीक्षण पर या सफाई चरण में वैसे भी गिरा दिया जाएगा। इसके अलावा, अगर आप का निर्माण लिपि में निर्माण डीबी उपयोग कर रहे हैं आप, आईडीई (कम से कम ग्रहण) से एक विशेष परीक्षा को चलाने के लिए है क्योंकि आप हाथ से परीक्षण डाटाबेस बनाने की आवश्यकता होगी क्षमता खो देते हैं। –

12

यह कुछ लगभग नगण्य ले लिया है, लेकिन मुझे लगता है, ड्रॉप बनाते हैं, और एच 2 और MySQL के लिए स्कीमा बनाने के लिए यह मिल गया। फिर भी गुण में और कुछ मामलों (जैसे एच 2 के रूप में) में ओरेकल और एसक्यूएल * सर्वर 2008 के लिए इसे पूरा मैं सही ड्रॉप tucked और बनाएं आदेशों की जरूरत है डेटाबेस बनाने पूर्णतः छोड़ दिया की जरूरत है। यहां यह दिखता है:

<plugin> 
    <!-- Used to automatically drop (if any) and create a database prior to running integration test cases. --> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>sql-maven-plugin</artifactId> 
    <dependencies> 
     <dependency> 
     <!-- Adds the correct JDBC driver as a dependency of this plugin --> 
     <groupId>${database.groupId}</groupId> 
     <artifactId>${database.artifactId}</artifactId> 
     <version>${database.version}</version> 
     </dependency> 
    </dependencies> 
    <configuration> 
     <!-- common configuration shared by all executions --> 
     <driver>${database.class}</driver> 
     <username>${database.username}</username> 
     <password>${database.password}</password> 
     <url>${database.url}</url> 
    </configuration> 
    <executions> 
     <execution> 
     <!-- Start by dropping the database (we'll leave it intact when finished) --> 
     <id>drop-db</id> 
     <phase>pre-integration-test</phase> 
     <goals> 
      <goal>execute</goal> 
     </goals> 
     <configuration> 
      <!-- Can't use regular URL in case database doesn't exist --> 
      <url>${database.url.alternate}</url> 
      <skip>${database.sqlDrop.skip}</skip> 
      <autocommit>true</autocommit> 
      <sqlCommand>${database.sqlDrop};</sqlCommand> 
      <onError>continue</onError> 
     </configuration> 
     </execution> 
     <execution> 
     <!-- then create a new database --> 
     <id>create-db</id> 
     <phase>pre-integration-test</phase> 
     <goals> 
      <goal>execute</goal> 
     </goals> 
     <configuration> 
      <!-- Can't use regular URL in case database doesn't exist --> 
      <url>${database.url.alternate}</url> 
      <skip>${database.sqlCreate.skip}</skip> 
      <autocommit>true</autocommit> 
      <sqlCommand>${database.sqlCreate};</sqlCommand> 
      <onError>continue</onError> 
     </configuration> 
     </execution> 
     <execution> 
     <!-- and finally run the schema creation script we just made with the hibernate3-maven-plugin --> 
     <id>create-schema</id> 
     <phase>pre-integration-test</phase> 
     <goals> 
      <goal>execute</goal> 
     </goals> 
     <configuration> 
      <skip>${database.sqlSchema.skip}</skip> 
      <autocommit>true</autocommit> 
      <srcFiles> 
      <srcFile>target/hibernate3/sql/create-${database.vendor}-schema.sql</srcFile> 
      </srcFiles> 
      <onError>continue</onError> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
संबंधित मुद्दे