首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有可能用MockMvc进行JPA测试吗?

有可能用MockMvc进行JPA测试吗?
EN

Stack Overflow用户
提问于 2016-02-05 07:04:05
回答 1查看 2.1K关注 0票数 1

我正在尝试使用spring测试来测试rest应用程序。

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

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

  1. 使用JPA将临时用户插入数据库
  2. 使用MockMvc执行请求控制器。
  3. 用预期的和实际的断言。
  4. 回滚临时用户。

此测试用例失败。可能到另一个执行环境(线程)?

代码语言:javascript
复制
@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()));
}

这可能是测试方案吗?

对另一个解决方案有什么帮助,或者如何使我的解决方案有效?

这是一个示例代码。

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

解出

我解决了这个异常。

异常点

  • 我用的是hikariCP。注意样本代码。 @Configuration @EnableAutoConfiguration @EnableTransactionManagement公共类JdbcConfig实现TransactionManagementConfigurer { @Autowired私有JdbcVO jdbcVO;@Bean public JdbcTemplate jdbcTemplate(){返回新的JdbcTemplate( DataSource ());}} @Bean public dataSource dataSource(){ final HikariDataSource dataSource=新HikariDataSource();} @Bean公共PlatformTransactionManager transactionManager(){返回新DataSourceTransactionManager(dataSource());}@覆盖公共PlatformTransactionManager annotationDrivenTransactionManager() {返回transactionManager();} }

当我使用手动数据源配置时发生错误。

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

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

发布于 2016-02-05 09:22:17

我会创建一个内存数据库,而不是模拟,您可以真实地测试一些东西。Hibernate将为您创建数据库。

示例配置如下所示

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35218157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档