Java 可以用MockMvc进行JPA测试吗?

Java 可以用MockMvc进行JPA测试吗?,java,spring,jpa,transactional,mockmvc,Java,Spring,Jpa,Transactional,Mockmvc,我正在尝试使用SpringTest测试rest应用程序 我有两个实体(用户、用户信息) (一对一关联,假设源和目标共享相同的主键值。) 这是我的测试场景。(在测试代码中) 使用JPA将临时用户插入数据库 使用MockMvc执行请求控制器 用期望值和实际值断言 回滚临时用户 此测试用例失败。 可能到另一个执行环境(线程) 是否可能出现测试场景 有其他解决方案的帮助吗,或者如何让我的解决方案起作用 这是一个示例代码 @Configuration @EnableAutoConfiguration @E

我正在尝试使用SpringTest测试rest应用程序

我有两个实体(用户、用户信息) (一对一关联,假设源和目标共享相同的主键值。)

这是我的测试场景。(在测试代码中)

  • 使用JPA将临时用户插入数据库
  • 使用MockMvc执行请求控制器
  • 用期望值和实际值断言
  • 回滚临时用户
  • 此测试用例失败。 可能到另一个执行环境(线程)

    是否可能出现测试场景

    有其他解决方案的帮助吗,或者如何让我的解决方案起作用

    这是一个示例代码

    @Configuration
    @EnableAutoConfiguration
    @EnableTransactionManagement
    public class JdbcConfig implements TransactionManagementConfigurer {
    
    @Autowired
    private JdbcVO jdbcVO;
    
    @Bean
    public JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource());
    }
    
    @Bean
    public DataSource dataSource(){
        final HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(jdbcVO.getDriver());
        dataSource.setJdbcUrl(jdbcVO.getUrl());
        dataSource.setUsername(jdbcVO.getUsername());
        dataSource.setPassword(jdbcVO.getPassword());
        return dataSource;
    }
    
    @Bean
    public PlatformTransactionManager transactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }
    
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return transactionManager();
    }
    


    解决了的 我解决了这个例外

    异常点

    • 我用hikariCP。观察示例代码

      @Configuration
      @EnableAutoConfiguration
      @EnableTransactionManagement
      public class JdbcConfig implements TransactionManagementConfigurer {
      
      @Autowired
      private JdbcVO jdbcVO;
      
      @Bean
      public JdbcTemplate jdbcTemplate(){
          return new JdbcTemplate(dataSource());
      }
      
      @Bean
      public DataSource dataSource(){
          final HikariDataSource dataSource = new HikariDataSource();
          dataSource.setDriverClassName(jdbcVO.getDriver());
          dataSource.setJdbcUrl(jdbcVO.getUrl());
          dataSource.setUsername(jdbcVO.getUsername());
          dataSource.setPassword(jdbcVO.getPassword());
          return dataSource;
      }
      
      @Bean
      public PlatformTransactionManager transactionManager(){
          return new DataSourceTransactionManager(dataSource());
      }
      
      @Override
      public PlatformTransactionManager annotationDrivenTransactionManager() {
          return transactionManager();
      }
      
      }

    手动使用数据源配置时出错

    因此,我在application.yml中更新了数据源配置

    spring: 
      jpa:
        database: mysql
        hibernate:
          naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
        #ddl-auto: create
        properties:
          hibernate.format_sql: true
        show-sql: true
    
      datasource:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test
        username: test
        password: test
    
    最后,我分享了这段代码。

    我会创建一个内存中的数据库,而不是mock,您可以真正地进行测试。Hibernate将为您创建数据库

    下面是配置示例

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
        <context:component-scan base-package="net.isban" />
        <tx:annotation-driven />
        <jpa:repositories base-package="net.isban" />
    
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.h2.Driver" />
            <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
            <property name="username" value="sa" />
            <property name="password" value="" />
        </bean>
    
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan" value="net.isban.example.entity" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                </props>
            </property>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    
    </beans>
    
    
    创造
    org.hibernate.dial.h2方言
    
    我将创建一个内存中的数据库,而不是mock,您可以真正地进行测试。Hibernate将为您创建数据库

    下面是配置示例

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
        <context:component-scan base-package="net.isban" />
        <tx:annotation-driven />
        <jpa:repositories base-package="net.isban" />
    
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="org.h2.Driver" />
            <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
            <property name="username" value="sa" />
            <property name="password" value="" />
        </bean>
    
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan" value="net.isban.example.entity" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                </props>
            </property>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    
    </beans>
    
    
    创造
    org.hibernate.dial.h2方言
    
    是的,这种测试是可能的。你得到了什么?例外?您在TestCase中没有断言,我会说没有,因为如果您使用“独立”mockmvc,就没有真正的spring上下文(没有数据源等),如果您使用另一个(有上下文的),您通常无法访问存储库/数据层。但是,可以将mock与独立的mockmvc一起使用,也可以将固定的DB(内存中的预定义数据)与另一个数据库一起使用。另请参见Jérémie B is rigth,这种测试是有效的,只需这样做,并且在测试时不要忘记@Transactional注释。是的,这种测试是可能的。你得到了什么?例外?您在TestCase中没有断言,我会说没有,因为如果您使用“独立”mockmvc,就没有真正的spring上下文(没有数据源等),如果您使用另一个(有上下文的),您通常无法访问存储库/数据层。但是,可以将mock与独立的mockmvc一起使用,也可以将固定的DB(内存中的预定义数据)与另一个数据库一起使用。另请参见Jérémie B is rigth,这种测试是有效的,只需这样做,并且在测试时不要忘记@Transactional注释。