I am new to testing the spring application, not sure how to test a application profiles defined application.yml in my spring boot app.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {WebApplication.class})
@WebAppConfiguration
public class ApplicationSettingsTest {
@Test
public void applicationPicksRightTeamProfile() throws Exception {
WebApplication.main(new String[] { "--spring.profiles.active=FalconDev1" });
String output = this.outputCapture.toString();
assertThat(output, containsString("falcondev.io"));
}
@Test
public void applicationPicksRightDefaultProfile() throws Exception {
WebApplication.main(new String[0]);
String output = this.outputCapture.toString();
assertThat(output, containsString("defaultdev.io"));
}
}
My first test appears to be passing, but the second tests is failed with multiple errors,
org.apache.catalina.LifecycleException: Failed to start component [Connector[org.apache.coyote.http11.Http11NioProtocol-8090]]
Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat"; Protocol handler start failed at org.apache.catalina.connector.Connector.startInternal(Connector.java:1014) ~[tomcat-embed-core-7.0.59.jar:7.0.59]
Caused by: java.net.BindException: Address already in use at sun.nio.ch.Net.bind(Native Method) ~[na:1.6.0_65] org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat servlet container at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:165) Caused by: java.lang.IllegalStateException: Tomcat connector in failed state at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:159) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
EDIT: application.yml
server:
port: ${port:8090}
I do understood that application has actually started on port 8090 and my second attempting to run again on the same port, which I don't want.
so how to tell in my tests to just load the application context rather starting the real application.Any ideas are appreciated.
WebApplication.main(new String[0])- the following code doesn't create Spring context. You do call justjavaclass. What functionality do you test? - Anton NovopashinEnvironmentfor that. See documentation docs.spring.io/spring/docs/current/javadoc-api/org/…. - Anton Novopashin