インテグレーションテストで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}"
}
@ dave-bowerさん、ありがとうございました。あなたの解決策が正しい方向に私を暗示してくれました。 – mawi
私の問題は、Spock-FrameworkとSpock-Springを使用していることです.Spock-Springでは、このプロパティが '@ Value'アノテーションを使用するときに解決しません。 私は@ContextConfiguration(loader = SpringApplicationContextLoader.class、classes = MyApplication.class)を使用してテストを初期化するためです。 質問を編集してSpock-Testing Environmentのソリューションを追加します。 – mawi
うれしかった! –