当前位置: 首页 > 知识库问答 >
问题:

Spring Boot 1.4.2、hibernate 5.2.3、存储库的Junit测试、@DataJpaTest问题、版本冲突?

邓韬
2023-03-14

我是Spring Boot的新手,我正在尝试测试一个存储库。这就是我所尝试的:

@RunWith(SpringRunner.class)
@DataJpaTest
public class MyRepositoryTest {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private MyRepository repository;

但我得到了以下例外:

java.lang.IllegalState异常:未能在org.springframework.test.context.cache.加载Application Context DefaultCacheAware ContextLoaderDelegate.loadContext(DefaultCacheAware ContextLoaderDelegate.java:124)...原因:org.springframework.beans.factory.BeanCreation异常:创建名称为'defaultServletHandlerMap'的bean时出错org.springframework.web.servlet.config.annotation.委托WebMvc配置:通过工厂方法的Bean实例化失败;嵌套异常org.springframework.beans.BeanInstantiation异常:未能实例化[org.springframework.web.servlet.HandlerMap]:工厂方法'defaultServletHandlerMap'抛出异常;嵌套异常java.lang.IllegalArgument异常:需要一个ServletContext来配置默认的servlet处理org.springframework.beans.factory.support.构造器esolver.instantiateUSingFactoryAct(构造器esolver.java:599)...原因:org.springframework.beans.BeanInstantiationExctive:未能实例化[org.springframework.web.servlet.HandlerMap]:工厂方法'defaultServletHandlerMap'抛出异常;嵌套异常java.lang.IllegalArgumentExctive:需要一个ServletContext来配置默认的servlet处理org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)...原因:java.lang.IllegalArgumentExctive:需要一个ServletContext来配置默认的servlet处理org.springframework.util.Asser. not Null(Asser. java: 115)在org. springframework. web. servlet. config. anNotation. DefaultServletHandlerConfigrer.(DefaultServletHandlerConfigrer. 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>

如您所见,由于版本冲突,我不得不排除hibernate entitymanager。这就是它不起作用的原因吗?

这是否意味着我必须切换回旧版本的hibernate才能工作?

我非常确定JUnit存储库测试需要我的应用程序JUnit。属性文件,用于定义H2 DB的设置,以及一些特定的表、索引和序列@DataJpaTest使用自动配置的内存数据库。所以这里还有一个问题:它会利用我的创意吗。h2。sql文件来设置模式?例如,通过阅读应用程序junit。自动属性?内容如下:

# database settings
spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle;INIT=create schema if not exists testdb\\;SET SCHEMA testdb\\;runscript from 'classpath:create.h2.sql';
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true

#datasource config
spring.database.driver-class-name=org.h2.Driver
spring.database.jndi-name=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true

如果您知道任何事情,请提前感谢,并让我知道:)!

更新

这是我的SpringBootApplication类:

@EnableWebMvc
@SpringBootApplication
public class ApplicationStarter {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationStarter.class, args);
    }
}

共有1个答案

彭浩穰
2023-03-14

您的@springboot应用程序上有@EnableWebMvc。当您使用切片注释(例如@DataJpaTest)时,Spring Boot通过查看@SpringBootConfiguration的测试包来查找要使用的上下文。如果没有找到,它会在父包中查找,等等。使用合理的包结构,没有进一步的定制,测试将使用@SpringBootApplication作为根上下文。

@EnableWebMvc如果您不打算完全控制MVC设置,则没有任何用处,有关更多详细信息,请参阅文档。

在你的情况下,这还有一个额外的副作用:现在每一个测试都需要基于网络,因为你正在强制mvc设置启动。

TL;DR永远不要在应用程序中添加这样的注释。只有在你真正需要的时候才放。

 类似资料:
  • 我最近把Spring Boot从2.3升级到了2.4。因为所有存储库测试都失败,并显示错误消息: 我的测试如下: 这是存储库类: 正如我所说,这发生在更新之后。在更新2.3.12版本的Spring Boot之前,一切正常。我已经搜索了发行说明,但找不到任何可能导致此问题的内容。任何人都可以帮忙吗?

  • 首先,我是Spring Hibernate开发的新手。遵循了很多教程书籍,我创建了一个主要基于Spring、Hibernate标准的示例应用程序,我已经开始为Repository(DAO)方法编写一些测试用例,即查找、查找、保存、删除。 当我执行测试类时,这真是太疯狂了,并不是所有的测试用例都能正确地为ex执行。尤其是find 当我完全执行以上所有测试用例时 甚至更新测试用例的行为也很奇怪,因为在

  • 我在使用spring存储库和服务的代码中遇到了这个问题,在我的spring项目中,它不适用于用户和角色,这是他们的服务和存储库的问题: 这是用户实现存储库: 这是用户的存储库和服务: 用户服务 试图消除空白,但这是同样的问题,我搜索了很多关于它,但没有找到任何解决方案。

  • 问题内容: 我有一个尝试获取某些文件的简单方法。我想测试文件何时不存在,这就是我的问题所在。测试不断失败。 该方法类似于: 在测试中,我尝试了两种单独的解决方案,但均未成功。 使用SO解决方案中建议的新样式 public ExpectedException exception = ExpectedException.none(); @Test public void testPopulateCon

  • mvn-x编译命令的输出 Apache Maven 3.0.4(R1232337;2012-01-17 14:14:56+0530)Maven Home:C:\java\apache-maven-3.0.4\bin.Java版本:1.6.0_37,供应商:Sun Microsystems Inc.Java Home:C:\程序文件\Java\JDK1.6.0_37\JRE默认区域设置:en_IN,