【问题标题】:Junit test cases for JPA repositoriesJPA 存储库的 Junit 测试用例
【发布时间】:2018-07-30 10:20:18
【问题描述】:

我是junit的新手,我有一个仓库如下:

@Repository
public interface ChartRepository extends JpaRepository<Chart, Integer>{
}

我的图表实体类如下:

@Entity
@Table(name = "Chart")
public class Chart {

    @Column(name = "ENT_ID")
    private String entID;

    @Column(name = "ent_NAME")
    private String entName;

    @Column(name = "ent_PRODUCER_ID")
    private String entProducerId;

    @Id
    @Column(name = "ent_Rc_ID")
    @SequenceGenerator(name = "ent_RC_ID_SEQ", sequenceName="ent_RC_ID_SEQ", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ent_RC_ID_SEQ")
    private Integer entReceiveId;

    @Column(name = "JOB_ID")
    private Integer jobId;

    @Column(name = "CREATE_DT")
    private Timestamp createdDate;

  //getters and Setters
}

现在,我们可以为存储库类编写测试用例了吗?如果是这样,我们该怎么做。谁能给我一些代码示例。

【问题讨论】:

    标签: spring-boot junit mockito


    【解决方案1】:

    您可以在其中创建一个@DataJpaTest@Autowire 您的存储库。例如:

    @RunWith(SpringRunner.class)
    @DataJpaTest
    public class MyJpaTest {
    
        @Autowired
        private ChartRepository chartRepository;
    
        @Test
        public void myTest() {
            ...
        }
    
    }
    

    查看更多信息:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test

    【讨论】:

      【解决方案2】:

      我强烈建议使用任何内存数据库来测试您的 JPA 存储库,并且不要使用 Mockito、EasyMock 等模拟测试框架。在 Dao 层中,不应该有任何业务逻辑可以模拟。它应该是简单的读/写操作。

      我为此使用 h2database。

      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(
            classes = {DatabaseConfig.class},
            loader = AnnotationConfigContextLoader.class)
      public class ChartRepositoryTest { 
      
        @Autowired
        private ChartRepository cartRepository;
      
        @Test 
        public void testfind() {
           // do find , insert and validate the response
        } 
      }
      
      
      testCompile('com.h2database:h2:1.4.196')
      

      这是数据库配置文件的样子

      @Configuration
      @EnableJpaRepositories(basePackages = "com.mypackage.repository")
      @PropertySource("application-test.properties")
      @EnableTransactionManagement
      public class DatabaseConfig {
      
      @Autowired
      private Environment env;
      
      @Bean
      public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
      
      dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.user"));
        dataSource.setPassword(env.getProperty("jdbc.pass"));
      
        return dataSource;
      }
      
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "com.mypackage.v2" });
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());
      
        return em;
      }
      
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
      
        return transactionManager;
      }
      
      Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "create");
        properties.setProperty("hibernate.dialect","org.hibernate.dialect.H2Dialect");
        return properties;
       }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-12
        • 2020-11-20
        • 2019-08-31
        • 2011-11-04
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        • 2015-06-23
        • 2018-06-26
        相关资源
        最近更新 更多