Java Spring Boot 1.4.2、hibernate 5.2.3、存储库的Junit测试、@DataJpaTest问题、版本冲突?
我是SpringBoot新手,我正在尝试测试一个存储库。 这就是我所尝试的:Java Spring Boot 1.4.2、hibernate 5.2.3、存储库的Junit测试、@DataJpaTest问题、版本冲突?,java,hibernate,spring-boot,junit,spring-data-jpa,Java,Hibernate,Spring Boot,Junit,Spring Data Jpa,我是SpringBoot新手,我正在尝试测试一个存储库。 这就是我所尝试的: @RunWith(SpringRunner.class) @DataJpaTest public class MyRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private MyRepository repository; 但我得到了以下例外: java.lang.I
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MyRepository repository;
但我得到了以下例外:
java.lang.IllegalStateException:未能加载ApplicationContext
位于org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
...
原因:org.springframework.beans.factory.BeanCreationException:创建名为“defaultServletHandlerMapping”的bean时出错,该名称在org.springframework.web.servlet.config.annotation.delegatingWebMVCCConfiguration中定义:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beanstantiationException:未能实例化[org.springframework.web.servlet.HandlerMapping]:工厂方法“defaultServletHandlerMapping”引发异常;嵌套异常是java.lang.IllegalArgumentException:配置默认servlet处理需要ServletContext
位于org.springframework.beans.factory.support.ConstructorResolver.InstanceUsingFactoryMethod(ConstructorResolver.java:599)
...
原因:org.springframework.beans.beans实例化异常:未能实例化[org.springframework.web.servlet.HandlerMapping]:工厂方法“defaultServletHandlerMapping”引发异常;嵌套异常是java.lang.IllegalArgumentException:配置默认servlet处理需要ServletContext
位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:189)
...
原因:java.lang.IllegalArgumentException:配置默认servlet处理需要ServletContext
位于org.springframework.util.Assert.notNull(Assert.java:115)
位于org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer。(DefaultServletHandlerConfigurer.java:53)
我有一些JUnit测试运行时没有问题,它们只使用以下注释:
@ActiveProfiles("junit")
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class MyControllerTest {
对于存储库Junit测试,我需要使用TestEntityManager来确保存储库完成了它的工作,而使用@DataJpaTest似乎是实现这一点的方法。但如上所述,我得到了一个例外
我尝试了一些注释的其他组合,但例外并没有消失
如果我的maven依赖项是错误的,那么它们是:
<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
....
</properties
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>
....
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
如果您知道任何事情,请提前感谢,并让我知道:)
更新
这是我的SpringBootApplication类:
@EnableWebMvc
@SpringBootApplication
public class ApplicationStarter {
public static void main(String[] args) {
SpringApplication.run(ApplicationStarter.class, args);
}
}
您的
@springboot应用程序上有@EnableWebMvc
。当您使用切片注释(例如@DataJpaTest
)时,springboot通过查看@SpringBootConfiguration
的测试包来查找要使用的上下文。如果找不到,则在父包中查找,等等。使用合理的包结构,无需进一步定制,测试将使用@SpringBootApplication
作为根上下文
@EnableWebMvc
如果您不打算完全控制MVC设置,则没有任何用处
在您的情况下,这还有一个额外的副作用:现在每个测试都需要基于web,因为您的测试正在强制mvc设置启动
TL;DR
切勿在应用程序上添加此类注释。只有在你真正需要的时候才放 这里有很多问题,我想我们可以回答所有的问题,但如果你能专注于一个问题,那就容易多了。您想知道如何将TestEntityManager
与SpringBootTest
一起使用吗?据我所知,如果我想测试存储库,我不需要加载整个spring上下文,所以我决定只使用@DataJpaTest,它只加载存储库测试所需的所有内容,而不使用SpringBootTest。所以我的问题是:为什么这对我不起作用?你没有解释什么不起作用。如果您在github上共享一个项目或其他东西,速度会快10倍。当我使用@DataJpaTest启动JUnit存储库测试时,我总是会遇到上述异常(无论是否使用SpringBootTest):未能加载ApplicationContext/创建名为“defaultServletHandlerMapping”的bean时出错/java.lang.IllegalArgumentException:配置默认servlet handlingOk需要ServletContext,我编辑了原始帖子,并更改了问题的顺序以使事情更清楚。多亏了,我还发现我必须添加spring webmvc,以便在没有@EnableWebMvc的情况下工作。现在例外情况消失了。我不知道你所说的“让它在没有”的情况下工作是什么意思,但我认为这并不准确。我已经添加了文档的链接,请看一下。非常感谢您的帮助,Stephane!如果没有SpringMVC,我还有一些例外,entityManagerFactory丢失了。我们可能应该在聊天中讨论这个问题(我想我知道你可能有什么问题)。加入我们。但不应在该上下文中添加该注释。
@EnableWebMvc
@SpringBootApplication
public class ApplicationStarter {
public static void main(String[] args) {
SpringApplication.run(ApplicationStarter.class, args);
}
}