2016-11-15 46 views
0
|--Integration tests 
    |--Spring boot rest application 

我有兩個模塊, 春天啓動的應用是我的終點, 它運行在自己的嵌入式tomcat的,我希望能夠運行它作爲集成測試的Maven構建的一部分,運行一體化測試它。如何從不同模塊運行spring-boot-rest應用程序,例如:在通過Maven構建的CI中?

我的問題是,有沒有辦法通過maven從不同的模塊運行spring引導應用程序?

在Spring引導的網站上,我只能看到一個使用spring-boot-maven-plugin通過自己的pom運行spring-boot應用程序的例子,但不能通過指定一個運行應用程序作爲不同模塊的一部分jar文件在執行中。

回答

0

是的,有幾種方法可以做到你的要求,例如:

  1. 使用@SpringBootTest標註上你的測試類(因​​爲春季啓動1.4);
  2. 以編程方式從您的測試中啓動Spring Boot應用程序。

第一個是我最喜歡的,也是比較簡單的一個,但它當然只適用於單元測試的環境。這是一個例子。

我們假設您的REST模塊中有一個名爲Application的類,其中註明了@SpringBootApplication。您可以通過只定義你的集成測試模塊內像這樣的試驗測試端點:

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = Application.class, properties = {"my.overriden.property=true"}) 
public class RestEndpointTest 
{ 
    // ... 
} 

通過這樣做,應用程序的整個環境將啓動。然後,您可以根據您的需要進一步配置您的測試,並覆蓋一些屬性(請參閱my.overridden.property)。

或者,您也可以定義自己的配置測試模塊內部,從另一模塊引用任何所需的類,例如:

@Configuration 
@ComponentScan(basePackageClasses = {BaseClass.class}) 
@EnableJpaRepositories 
@EntityScan 
@EnableAutoConfiguration 
public class SupportConfiguration 
{ 
    @Bean 
    public ARequiredBean bean() 
    { 
     return new ARequiredBean(); 
    } 

    // etc... 
} 

和使用它就像你會與任何其他背景信息:

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes = SupportConfiguration.class) 
public class CustomTest 
{ 
    // ... 
} 

另一種方法是通過編程啓動REST應用的實例,像這樣的東西:

public static void main(String[] args) throws IOException 
    { 
     try (ConfigurableApplicationContext context = SpringApplication.run(Application.class, args)) 
     { 
      log.info("Server Started. Press <Enter> to shutdown..."); 
      context.registerShutdownHook(); 
      BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in)); 
      inReader.readLine(); 
      log.info("Closing application context..."); 
      context.stop(); 
     } 
     log.info("Context closed, shutting down. Bye."); 
     System.exit(0); 
    } 
相關問題