unit testing - Spring Boot profile. How to test? -


how can test profiles?

that's test

    @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 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 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 active: uat"));     assert.assertfalse(output.contains("the following profiles active: dev"));     assert.assertfalse(output.contains("the following profiles active: default")); } 

my first test executes ok others fail.

org.springframework.jmx.export.unabletoregistermbeanexception: unable register mbean [org.springframework.boot.actuate.endpoint.jmx.dataendpointmbean@6cbe68e9] key 'requestmappingendpoint'; nested exception javax.management.instancealreadyexistsexception: org.springframework.boot:type=endpoint,name=requestmappingendpoint 

how can stop instance before next test start? or better approach?

thanks

i way:

@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);     } } 

the code above asumes have application-first.properties this:

someproperty=first-value 

i have 1 test class per profile wish test, 1 above profile first. if must have test on active profiles (rather property), may this:

@autowired private environment environment;  @test public void testactiveprofiles() {     assertarrayequals(new string[]{"first"}, environment.getactiveprofiles());       } 

Comments