2017-04-19 9 views
1

私のアプリケーションにはSpringブート1.5が使用されています。統合テストでは、Webサーバーのランタイムポート番号を取得したい(メモ:TestRestTemplateは私の場合は役に立ちません)。私が試したアプローチはいくつかありますが、どれもうまくいかないようです。私のアプローチは以下の通りです。私はSpringブートテスト1.5でランタイムローカルサーバーポートを設定できません

はserver.port = 8081

としてサーバのポートを定義している。しかし、このコードでは、私はエラーを取得しています私のsrc/main/resources/config/application.propertiesファイルで

最初のアプローチ

@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.DEFINED_PORT) 
public class RestServiceTest { 

@LocalServerPort  
protected int port; 

が値にプレースホルダ 'local.server.port' を解決できませんでした "$ {} local.server.port" 私は

を変更した

第二のアプローチ

webEnvironment = WebEnvironment.DEFINED_PORT

~

webEnvironment = WebEnvironment.RANDOM_PORT

と私のsrc/main/resources/config/application.propertiesファイルに私はこれが最初のようにエラーと同じようにスロー

はserver.port = 0

を定義していますアプローチ。

第三のアプローチは第三のアプローチでは

私はこれが最後に私がApplicationEventsを使用しようとしましたnull

第四のアプローチ

を返し

protected int port; 

@Autowired 
Environment environment 

this.port = this.environment.getProperty("local.server.port"); 

使用しようとしました〜へEmbeddedServletContainerIntialize

@EventListener(EmbeddedServletContainerInitializedEvent.class) 
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { 
this.port = event.getEmbeddedServletContainer().getPort(); 
} 

public int getPort() { 
return this.port; 
} 

は、今TestConfig

に同じを追加しました私のテストクラスでは、私は、このリスナーがポートに

@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.RANDOM_PORT) 
public class RestServiceTest { 

protected int port; 

@Autowired 
EmbeddedServletContainerIntializedEventListener embeddedServletcontainerPort; 

this.port = this.embeddedServletcontainerPort.getPort(); 
を取得するために使用することを試みているに耳を傾けるためにイベントリスナーを作成することで、ポート番号を見つけます

これは0を返します。また、私はリスナーイベントが決して引き起こされないことを知りました。

これはドキュメントや他の投稿のように非常に単純ですが、どういうわけか私のために働いていません。ヘルプは非常に感謝しています。

+0

'TestCnfig'は実際のアプリケーションクラスですか?それがロードされるべきもの( '@ SpringBootAPplication'アノテーションを含み、すべてのものをトリガするもの)です。 –

+0

@MDeinum 'TestConfig'は、他のBeanが定義されているテスト環境に固有の' @Configuration'ファイルです。 –

+0

それから動作しません。完全なアプリケーション構成が必要です。それ以外の場合、アプリケーションは起動も自動構成も行われないため、ポートは設定されません。 –

答えて

0

テスト用Web環境用のランダムポートを設定していないことがあります。ちょうど春のブート1.5.2で正常に実行されました。ここ @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)

テスト:

この

は、トリックを行う必要があり

import static org.hamcrest.Matchers.greaterThan; 
import static org.junit.Assert.assertThat; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 
import org.springframework.test.context.junit4.SpringRunner; 

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 
public class RandomPortTests { 

    @Value("${local.server.port}") 
    protected int localPort; 

    @Test 
    public void getPort() { 
     assertThat("Should get a random port greater than zero!", localPort, greaterThan(0)); 
    } 

} 
0

は、あなたのクラスの装飾の上@RunWith(SpringRunner.class)を入れるのを忘れていました。

だから、これを試してください。

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = TestConfig.class, webEnvironment =WebEnvironment.DEFINED_PORT) 
public class RestServiceTest { 
    @LocalServerPort 
    int randomServerPort; 
    ... 
} 
関連する問題