2016-08-08 2 views
1

プロファイルをテストするにはどうすればよいですか?私のテストスプリングブートプロファイル。どのようにテストする?

@Test 
public void testDevProfile() throws Exception { 
    System.setProperty("spring.profiles.active", "dev"); 
    Application.main(new String[0]); 
    String output = this.outputCapture.toString(); 
    Assert.assertTrue(output.contains("The following profiles are active: dev")); 
} 

@Test 
public void testUatProfile() throws Exception { 
    System.setProperty("spring.profiles.active", "uat"); 
    Application.main(new String[0]); 
    String output = this.outputCapture.toString(); 
    Assert.assertTrue(output.contains("The following profiles are active: uat")); 
} 

@Test 
public void testPrdProfile() throws Exception { 
    System.setProperty("spring.profiles.active", "prd"); 
    Application.main(new String[0]); 
    String output = this.outputCapture.toString(); 
    Assert.assertFalse(output.contains("The following profiles are active: uat")); 
    Assert.assertFalse(output.contains("The following profiles are active: dev")); 
    Assert.assertFalse(output.contains("The following profiles are active: default")); 
} 

私の最初のテストはOK実行しますが、他の人が失敗だ

org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [[email protected]6cbe68e9] with key 'requestMappingEndpoint'; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Endpoint,name=requestMappingEndpoint 

次のテスト開始前にインスタンスを停止するにはどうすればよいですか?それとも良いアプローチですか?

おかげ

答えて

4

が、私はそれをこのようにします:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = SpringBootApp.class) 
@ActiveProfiles("first") 
public class ProfileFirstTest { 

    @Value("${someProperty}") 
    private String someProperty; 

    @Test 
    public void testProperty() { 
     assertEquals("first-value", someProperty); 
    } 
} 

あなたはこのようなapplication-first.properties持っasumes上記のコード:私は1つのテストクラスあたりを持っているでしょう

someProperty=first-value 

をあなたがテストしたいプロファイル、上記はプロファイルの最初のです。 には、プロパティではなくアクティブなプロファイルをテストする必要があります。

@Autowired 
private Environment environment; 

@Test 
public void testActiveProfiles() { 
    assertArrayEquals(new String[]{"first"}, environment.getActiveProfiles());  
} 
関連する問題