2016-04-12 25 views
2

インテグレーションテストでmanagement.portプロパティを0に設定すると、アクチュエータエンドポイントに対応する埋め込み型のTomcatに割り当てられたポートを取得する方法に関するアドバイスを探しています。私の統合テストは、その後0実行時に実行時にSpringブート管理ポートを取得する

@WebIntegrationTest({ "server.port=0", "management.port=0" }) 

と、次の上に示したポートを設定し、@WebIntegrationTestでアノテートされている

server.port: 8080 
server.contextPath: /my-app-context-path 

management.port: 8081 
management.context-path: /manage 

... 

:以下application.yml構成で春ブーツ1.3.2を使用して

イム完全な統合テストを行うときは、ユーティリティー・クラスを使用してアプリケーション構成にアクセスする必要があります。

@Component 
@Profile("testing") 
class TestserverInfo { 

    @Value('${server.contextPath:}') 
    private String contextPath; 

    @Autowired 
    private EmbeddedWebApplicationContext server; 

    @Autowired 
    private ManagementServerProperties managementServerProperties 


    public String getBasePath() { 
     final int serverPort = server.embeddedServletContainer.port 

     return "http://localhost:${serverPort}${contextPath}" 
    } 

    public String getManagementPath() { 
     // The following wont work here: 
     // server.embeddedServletContainer.port -> regular server port 
     // management.port -> is zero just as server.port as i want random ports 

     final int managementPort = // how can i get this one ? 
     final String managementPath = managementServerProperties.getContextPath() 

     return "http://localhost:${managementPort}${managementPath}" 
    } 
} 

標準ポートはlocal.server.portを使用して取得でき、管理エンドポイントがlocal.management.portであると思われます。しかし、それは別の意味を持っているようです。

編集: 公式ドキュメントには、これを行う方法について言及していない:(http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-discover-the-http-port-at-runtime

は、現在、その管理ポートに手を取得する任意の文書化されていない方法はありますか?


ソリューション編集:私は私の春ブートアプリケーションをテストするためスポック・フレームワークとスポックスプリングを使用していますように、私が使用してアプリケーションを初期化する必要があり

@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyApplication.class) 

どういうわけかSpock-Springやテストの初期化が@Value注釈の評価に影響しているように、@Value("${local.management.port}")

私はプロパティが存在していた知っていたので、私は単純に、テスト実行時にプロパティ値を取得するために、直接、春 Environmentを使用するソリューションでは
java.lang.IllegalArgumentException: Could not resolve placeholder 'local.management.port' in string value "${local.management.port}" 

@Autowired 
ManagementServerProperties managementServerProperties 

@Autowired 
Environment environment 

public String getManagementPath() { 
    final int managementPort = environment.getProperty('local.management.port', Integer.class) 
    final String managementPath = managementServerProperties.getContextPath() 

    return "http://localhost:${managementPort}${managementPath}" 
} 

答えて

4

これは私がそれをやった方法です、ストレートコピー私のテストクラス(私はアサーションのためRestAssuredを使用)から:春ブーツのよう

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.test.SpringApplicationConfiguration; 
import org.springframework.boot.test.WebIntegrationTest; 

import org.springframework.test.annotation.DirtiesContext; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

import static com.jayway.restassured.RestAssured.get; 
import static org.hamcrest.CoreMatchers.equalTo; 

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(Application.class) 
@WebIntegrationTest(randomPort = true, value = {"management.port=0", "management.context-path=/admin"}) 
@DirtiesContext 
public class ActuatorEndpointTest { 

    @Value("${local.management.port}") 
    private int localManagementPort; 

    @Test 
    public void actuatorHealthEndpointIsAvailable() throws Exception { 

     String healthUrl = "http://localhost:" + localManagementPort + "/admin/health"; 
     get(healthUrl) 
       .then() 
       .assertThat().body("status", equalTo("UP")); 
    } 



} 
+0

@ dave-bowerさん、ありがとうございました。あなたの解決策が正しい方向に私を暗示してくれました。 – mawi

+0

私の問題は、Spock-FrameworkとSpock-Springを使用していることです.Spock-Springでは、このプロパティが '@ Value'アノテーションを使用するときに解決しません。 私は@ContextConfiguration(loader = SpringApplicationContextLoader.class、classes = MyApplication.class)を使用してテストを初期化するためです。 質問を編集してSpock-Testing Environmentのソリューションを追加します。 – mawi

+0

うれしかった! –

4

もっと簡単な方法がある1.4.0:

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { 
    "management.port=0", "management.context-path=/admin" }) 
@DirtiesContext 
public class SampleTest { 

    @LocalServerPort 
    int port; 

    @LocalManagementPort 
    int managementPort; 
+0

これにはorg.springframework.boot.testをインポートする必要があります。context.SpringBootTest.WebEnvironment; –

関連する問題