2016-08-19 8 views
4

私はspring-bootのITテストを作成しています。私はここでdevプロファイルスプリングブートのプロファイルに基づいて選択テストを実行/停止する方法

をアクティブにしたとき

テスト用のデータがapplication-dev.propertiesから来ていることは、私はテストのために持っているものです。

@RunWith(SpringRunner.class) 
@SpringBootTest 
@WebAppConfiguration 
public class ApplicationTests { 

    @Autowired 
    Environment env; 

    @Test 
    public void contextLoads() { 
     System.out.println(Arrays.toString((env.getActiveProfiles()))); 

    } 

} 

ServiceITTest

public class ServiceITTest extends ApplicationTests { 


    @value 
    String username; 

    @value 
    String address; 

    @Autowired 
    MyService myService; 


     @Test 
     public void check_for_valid_username_address(){ 
      myService.validate(username,address); 
     } 
} 

私は上記をしたいです私が "dev"、 "qa"のプロファイルを設定したときにのみテストを実行します。デフォルトでは実行されません。

春のブートテストでその細かいコントロールを取得することは可能ですか?

答えて

6

@IfProfileValueアノテーションを使用します。残念ながら、アクティブなプロファイルでは直接動作しませんが、プロパティを読み取ることができます。テスト内で実行するプロファイル内の特定のプロパティのみを定義すると、その特定のプロパティでその注釈を使用できます。

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#integration-testing-annotations-junit

+0

これは良いものです。役に立った –

関連する問題