【问题标题】:How to test Hibernate Configuration Class which is Java Annotated in SpringBoot using Mockito?如何使用 Mockito 在 Spring Boot 中测试作为 Java 注释的 Hibernate 配置类?
【发布时间】:2019-03-19 07:52:33
【问题描述】:

我正在使用 Mysql DB 开发一个 SPringBoot+Hibernate+REST 项目。我想知道如何对下面的课程进行单元测试。有人告诉我,Mocking 是最好的方法。我认为这与数据库模拟测试有关。即使在网上研究了很多之后,我也不知道。有人可以指导我如何测试它。如果我的项目中有使用这些数据库连接的 DAO 类,我还需要测试下面的类吗?我什至有需要测试的 RestController 类。请指导我应该如何进行。我是初学者。

DBConfiguration.Java

package com.youtube.project.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

@PropertySource(value = { "classpath:application.properties" })
@Configuration
@EnableTransactionManagement
public class DBConfiguration {

    @Value("${jdbc.driverClassName}")
    private String driverClass;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${hibernate.dialect}")
    private String dialect;

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(url,username,password);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
        factory.setDataSource(getDataSource());
        factory.setHibernateProperties(hibernateProperties());
        factory.setPackagesToScan(new String[] {"com.youtube.project"});
        return factory;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", dialect);
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");
        return properties;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory factory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(factory);
        return transactionManager;
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(getDataSource());
          em.setPackagesToScan(new String[] { "com.youtube.project.model" });     
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          return em;
       }
} 

【问题讨论】:

    标签: spring unit-testing spring-boot mocking mockito


    【解决方案1】:

    我想知道如何对下面的类进行单元测试

    嗯,这有点复杂,因为您的配置类实际上会连接到数据库,所以这个类实际上对于“单元测试”本身并不是很理想。

    因为单元测试是软件测试的一个级别,其中测试单个单元/组件(单元是应用程序中最小的可测试部分)。它通常有一个或几个输入,通常只有一个输出。

    恕我直言,我会将其作为集成测试,因为您需要连接到数据库。

    如何进行测试:

    TL;DR - 不要费心为这个类编写显式测试。

    通过在您的主类上运行@SpringBootTest,您的类应该作为副作用进行测试。

    说明:

    明确地测试这个类没有什么意义,因为你基本上会测试:

    • bean 是否已创建
    • @Value 字段是否填充了值
    • 与数据库的连接是否已建立

    从这 3 点来看,只有第 3 点是有效的测试,前两点是 Spring 开发人员在编写框架时测试的。话虽如此,连接到数据库更多的是集成测试而不是单元测试。如果你想测试它,你可以使用像 H2 这样的内存数据库,它可以配置为仅在测试时运行。

    我的项目中有使用这些数据库连接的 DAO 类,是吗? 还需要测试下面的类吗?我什至有 RestController 类 需要测试。

    Spring 对测试应用程序切片(应用程序的一部分,例如:一次一个类或一个类 + 它的依赖项)提供了很好的支持。

    具体来说:

    • 对于 DAO 类,请查看 @DataJpaTestTestEntityManager 类。
    • 对于控制器类,请使用 @WebMvcTestMockMvc 类。

    这些东西旨在让您使用 Spring Boot 更轻松地进行测试。有关一些基本信息,您可以查看this 文章。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2018-02-11
      • 2021-05-22
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2018-01-27
      • 2017-02-05
      相关资源
      最近更新 更多