- I have a java/spring-boot class
@SpringBootApplication
@ComponentScan(value = "ignore")
@RunWith(SpringRunner.class)
public class MyApplication {
@Bean
YAMLConfig myeadFromYMLFile() {
return new YAMLConfig();
}
public static void main(String[] args) throws InterruptedException {
ConfigurableApplicationContext context =
SpringApplication.run(MyApplication.class, args);
YAMLConfig myBean = context.getBean(YAMLConfig.class);
myBean.doSomething();
myBean.printApi();
}
}
- And have config file, what reads data from yml file
@Configuration
@EnableConfigurationProperties
public class YAMLConfig {
@Value("${refresh.rate}")
private int refreshRate;
@Value("${datasource.apiUrl}")
private String apiUrl;
public void doSomething() {
System.out.printf("Refresh Rate : %s%n", refreshRate);
}
public void printApi() {
System.out.printf("Refresh Rate : %s%n", apiUrl);
}
public String getApiUrl() {
return apiUrl;
}
}
Question: when I run class MyApplication in src/java/main everything is cool. BUT: when I run this class in src/java/test it fails
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-09-06 17:14:21.389 ERROR 16515 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.NoClassDefFoundError: javax/servlet/http/HttpSessionIdListener at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at MyApplication.main(MyApplication.java:19) [test-classes/:na] Caused by: java.lang.NoClassDefFoundError: javax/servlet/http/HttpSessionIdListener at io.undertow.servlet.core.ApplicationListeners.(ApplicationListeners.java:62) ~[undertow-servlet-1.4.25.Final.jar:1.4.25.Final] at io.undertow.servlet.spec.ServletContextImpl.ensureNotProgramaticListener(ServletContextImpl.java:875) ~[undertow-servlet-1.4.25.Final.jar:1.4.25.Final] at io.undertow.servlet.spec.ServletContextImpl.getSessionCookieConfig(ServletContextImpl.java:648) ~[undertow-servlet-1.4.25.Final.jar:1.4.25.Final] at io.undertow.servlet.core.DeploymentManagerImpl.handleDeploymentSessionConfig(DeploymentManagerImpl.java:622) ~[undertow-servlet-1.4.25.Final.jar:1.4.25.Final] at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:167) ~[undertow-servlet-1.4.25.Final.jar:1.4.25.Final] at org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory.createDeploymentManager(UndertowServletWebServerFactory.java:284) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory.getWebServer(UndertowServletWebServerFactory.java:208) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] ... 8 common frames omitted Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpSessionIdListener at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_181] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_181] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_181] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_181] ... 17 common frames omitted
P.S.I have spring-boot-starter-web dependency. With @SpringBootTest I got this:
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at MyApplication.main(MyApplication.java:19) [test-classes/:na] Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] ... 8 common frames omitted
MyApplication
class both insrc/java/test
and insrc/java/main
? Is it a typo, or you don't follow the Maven Standard Directory Layout? – Borismain
in your test class. As said by Boris you should not mix two things. For your application, you can havemain
. For test classes@Runwith
and@Test
should be sufficient – Ganesh Satpute