2015-04-17 11 views
7

मैंने स्प्रिंग बूट और रीस्टफुल सेवाओं के साथ काम करने के बारे में कई मार्गदर्शिकाएं पढ़ी हैं, और उनमें से कई में यूनिट परीक्षण चलाने के बारे में जानकारी है, विशेष रूप से "स्प्रिंग बूट के साथ एक एप्लीकेशन बनाना"। हालांकि, मैंने कुछ भी नहीं देखा है जो क्लाउड माइक्रो-सर्विसेज आर्किटेक्चर में सामान्य है, जैसा कि अन्य स्प्रिंग बूट अनुप्रयोगों पर उपभोग/निर्भर करता है, एक स्प्रिंग बूट एप्लिकेशन का परीक्षण करने के तरीके पर एक उदाहरण देता है।एकीकरण परीक्षण स्प्रिंग बूट आधारित माइक्रोस्कोर्सेस

ServiceMediator, Adapter1, Adapter2

ServiceMediator कॉल Adapter1 या Adapter2, इनपुट के आधार पर: तो, उदाहरण के लिए, हम निम्नलिखित वसंत बूट सेवाओं की है।

स्प्रिंग जुनीट परीक्षण में ServiceMediator को शुरू करने और परीक्षण करने से पहले स्प्रिंग बूट सेवाओं एडाप्टर 1 और एडाप्टर 2 को शुरू करने का कोई तरीका है?

+0

आप कर रहे हैं बात कर रहे यूनिट टेस्ट के लिए जहां आप सेवा मेडिएटर का मज़ाक उड़ाएंगे या आप एक इंटीग्रेशन टेस्ट करना चाहते हैं, जहां आप असली सेवा कहां कॉल करते हैं? – ndrone

+0

एकीकरण परीक्षण वह है जिसके बारे में मैं बात कर रहा हूं। – LouRoy

+0

मुझे लगता है कि यह वास्तव में इस पर निर्भर करता है कि आप इसे कैसे परिभाषित करते हैं। मैं बस एक स्वचालित फैशन में अपनी स्थानीय मशीन पर सभी सेवाओं का परीक्षण करने का एक तरीका ढूंढ रहा हूं। इसलिए, क्या मध्यस्थों की जांच करने से पहले मध्यस्थ सेवाओं को शुरू करने का कोई तरीका है? – LouRoy

उत्तर

1
package controller; 


import static org.junit.Assert.assertThat; 

import java.io.File; 
import java.net.URL; 

import mediator.CLPApplication; 

import org.hamcrest.Matchers; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.test.IntegrationTest; 
import org.springframework.boot.test.SpringApplicationConfiguration; 
import org.springframework.boot.test.TestRestTemplate; 
import org.springframework.http.ResponseEntity; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 
import org.springframework.web.client.RestClientException; 
import org.springframework.web.client.RestTemplate; 

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = CLPApplication.class) 
@WebAppConfiguration 
@IntegrationTest() 
public class ControllerTest { 

    @Value("${adapter.dependency.jar.location}") 
    private String adapterDependencyJarLocation; 

    @Value("${adapter.dependency.jar.name}") 
    private String adapterDependencyJarName; 

    @Value("${adapter.url}") 
    private String adapterURL; 

    @Value("${mediator.url}") 
    private String mediatorURL; 

    private URL mediator; 
    private URL adapter; 
    private RestTemplate template; 
    Process process = null; 

    @Before 
    public void setUp() throws Exception { 

     adapter = new URL(adapterURL); 
     template = new TestRestTemplate(); 

     // 
     // Start the Atomic adapter 
     // 
     System.out.println(adapterDependencyJarLocation); 
     System.out.println("Starting Adapter"); 

     try { 
      process = new ProcessBuilder("java", "-jar", adapterDependencyJarName) 
       .directory(new File(adapterDependencyJarLocation)).start(); 

      // Try connecting 5 times with a 5 second pause between each 
      // to see if it started. 
      Thread.sleep(5000); 
      for(int i = 0; i <= 5; i++) { 
       try{ 
        System.out.println("Testing to see if Adapter is up"); 
        template.getForEntity(adapter.toString(), String.class); 
        System.out.println("Adapter Started"); 
        break; 
       } 
       catch(RestClientException rce){ 
        System.out.println("It's not up yet"); 
       } 
       Thread.sleep(5000); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Test 
    public void testMediator() throws Exception { 
     mediator = new URL(mediatorURL); 
     System.out.println("Calling Mediator"); 
     ResponseEntity<String> response = template.getForEntity(mediator.toString(), String.class); 
     System.out.println(response.getBody()); 
     // Getting back JSON, so check to see if it starts with an open bracket 
     assertThat(response.getBody(), Matchers.startsWith("{")); 
    } 

    @After 
    public void tearDown() { 
     if(process != null) { 
      process.destroy(); 
     } 
    } 
} 
4

रूप पूर्व एकीकरण परीक्षण चरण में एकाधिक जावा प्रक्रियाओं (मानक स्प्रिंग बूट एप्लिकेशन) शुरू करने के लिए अनुमति देता है process-exec-maven-plugin सहायक हो सकता है, और यह अपने आप पोस्ट में उन्हें नीचे बंद का ख्याल रखता है एकीकरण-परीक्षण चरण।

नोट: एकीकरण परीक्षण के लिए है कि Maven-फेल सेफ-प्लगइनवसंत-बूट-Maven-प्लगइनsee साथ कॉन्फ़िगर किया जाना चाहिए एकीकरण परीक्षण चरण में चला जाना चाहिए। फिर, चलाने के लिए हमारे एकीकरण परीक्षणों की पुष्टि या उच्चतर Maven जीवनचक्र लक्षित किया जाना चाहिए, क्योंकि एकीकरण परीक्षण चरण पैकेज और lifecycles see Default Lifecycles की पुष्टि के बीच स्थित वास्तव में है।

निम्नलिखित Maven (pom.xml) विन्यास मेरे लिए काम किया:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <version>1.3.5.RELEASE</version> 
      <executions>      
       <execution> 
        <id>pre-integration-test</id> 
        <goals> 
         <goal>start</goal> 
        </goals> 
        <configuration> 
         <skip>${integration-tests.skip}</skip> 
        </configuration> 
       </execution> 
       <execution> 
        <id>post-integration-test</id> 
        <goals> 
         <goal>stop</goal> 
        </goals> 
        <configuration> 
         <skip>${integration-tests.skip}</skip> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin>   

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-failsafe-plugin</artifactId> 
      <version>2.19.1</version> 
      <configuration> 
       <skip>${integration-tests.skip}</skip>     
       <includes> 
        <include>**/*IT.java</include> 
       </includes> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>integration-test</goal> 
         <goal>verify</goal> 
        </goals>       
       </execution>      
      </executions> 
     </plugin> 

     <plugin> 
      <groupId>com.bazaarvoice.maven.plugins</groupId> 
      <artifactId>process-exec-maven-plugin</artifactId> 
      <version>0.7</version> 
      <executions>      
       <execution> 
        <id>switchboard-process</id> 
        <phase>pre-integration-test</phase> 
        <goals> 
         <goal>start</goal> 
        </goals> 
        <configuration> 
         <name>accounts-service</name> 
         <workingDir>/../../micro-service</workingDir> 
         <waitForInterrupt>false</waitForInterrupt>       
         <arguments> 
          <argument>java</argument> 
          <argument>-jar</argument> 
          <argument>${basedir}/../micro-service/target/micro-service-${project.version}-exec.jar</argument> 
         </arguments> 
        </configuration> 
       </execution> 
       <!--Stop all processes in reverse order--> 
       <execution> 
        <id>stop-all</id> 
        <phase>post-integration-test</phase> 
        <goals> 
         <goal>stop-all</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

एक एकता टेस्ट वर्ग होने test.java में (WebServerIT) फ़ोल्डर:

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = WebServerApp.class) 
@WebIntegrationTest("server.port:0") 
public class WebServerIT { 

    @Autowired 
    private WebApplicationContext webServerAppContext; 

    private MockMvc webServerMockMvc; 

    @Before 
    public void setUp() { 
     System.out.println("the test is set up"); 
     webServerMockMvc = MockMvcBuilders.webAppContextSetup(webServerAppContext).build(); 
    } 

    /** 
    * This test calls the WebServer's endpoint (/accounts/123456789) which in turn calls the micro-service rest api 
    * which is started using the process-exec-maven-plugin, otherwise the test would fail. 
    */ 
    @Test 
    public void testWebServerInteractionWithMicroService() throws Exception { 
     this.webServerMockMvc.perform(get("/accounts/123456789")) 
       .andExpect(status().isOk()); 
    } 
} 
संबंधित मुद्दे