【发布时间】:2018-10-12 21:11:03
【问题描述】:
我正在尝试按照 Spring 网站上的示例测试我的应用程序。
这些是我的依赖项:
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile("org.springframework.boot:spring-boot-starter-security")
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4")
runtime('org.springframework.boot:spring-boot-devtools')
runtime('com.h2database:h2')
runtime('mysql:mysql-connector-java')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
这是测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomePage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk());
}
}
我得到错误:
org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
java.lang.IllegalStateException: 无法加载 ApplicationContext
知道是什么原因造成的吗?我没有任何特定的配置文件,只有安全配置和 webconfig。
网络配置:
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**", "/css/**", "/fragments/**")
.addResourceLocations("classpath:/static/images/", "classpath:/static/css/", "classpath:/fragments/");
}
}
【问题讨论】:
-
请发布
IllegalStateException的根本原因/堆栈跟踪。原因在于第一个异常发生,ISE 是多个异常链接的结果。 -
@Antot 更新主帖
-
看来您需要将
javax.xml.bind:jaxb-api添加到依赖项列表中。 -
听起来您可能在 JDK 9 或更高版本上运行?这是有意的,还是切换到 JDK 8 适合您?
标签: java spring spring-boot spring-test spring-test-mvc