是否可以使用MockMvc进行JPA测试?

时间:2016-02-05 07:04:05

标签: java spring jpa transactional mockmvc

我尝试使用弹簧测试来测试休息应用程序。

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

这是我的测试方案。 (在测试代码中)

  1. 使用JPA
  2. 将临时用户插入数据库
  3. 使用MockMvc执行请求控制器。
  4. 断言与预期和实际。
  5. 回滚临时用户。
  6. 此测试用例失败。 也许到另一个执行环境(线程)??

    metric

    是否有可能测试场景?

    有关其他解决方案的任何帮助或如何使我的解决方案正常运行?

    这是一个示例代码。

    https://gist.github.com/okihouse/f5e2fe8fa4c17d6a6be9

    解决

    我解决了这个例外。

    例外点

    • 我使用了hikariCP。观看示例代码。

      @Test
      public void test() throws Exception {
          // create temporary user for test.
          User user = new User();
          user.setType(Type.User);
      
          UserInfo userInfo = new UserInfo();
          userInfo.setEmail("temporary_user@test.com");
          userInfo.setUser(user);
      
          user.setUserInfo(userInfo);
          // persist
          userRepository.save(user);
      
          // request post
          mockMvc.perform(
                  post("/user")
                  .param("email", "temporary_user@test.com")
                  .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                  .andExpect(status().isOk())
                  .andExpect(jsonPath("$.email", userInfo.getEmail()));
      }
      

      }

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

    因此,我在 application.yml

    中更新了数据源配置
    @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();
    }
    

    最后,我分享了这段代码。 https://github.com/okihouse/spring-jpa-test

1 个答案:

答案 0 :(得分:0)

我会创建一个内存数据库,而不是模拟,你可以测试真实的东西。 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>