其他分享
首页 > 其他分享> > 项目中首次使用DBUnit拾遗

项目中首次使用DBUnit拾遗

作者:互联网


文章目录


参考

正文

测试准备

测试数据准备

dev_equipment_info.xlsx - 测试数据

expected_dev_equipment_info.xlsx - 测试后数据

Excel数据准备注意点

Maven 包导入

在pom.xml的dependencies中,增加

com.github.yangjianzhouspring-boot-unitils-starter1.3.0.RELEASE

Unitils额外配置(数据库连接)

application-ut.properties 存放于resources下,其内容可参考

database.driverClassName=com.mysql.jdbc.Driver
# 此数据库连接信息
database.url=jdbc:mysql://127.0.0.1/septictank_device
# 此数据库连接用户名
database.userName=root
# 此数据库连接用户密码
database.password=123456
# 此数据库连接的schema
database.schemaNames=septictank_device
# 此数据库数据库类型:oracle/mysql/postgres等
database.dialect=mysql
dataSource.beanName=druidDataSource

测试类配置 - BusDeviceTestApp

package com.citylink.device;

import com.unitils.boot.autoconfigure.ConfigurableApplicationContextAware;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.citylink.device"}, basePackageClasses =
        {ConfigurableApplicationContextAware.class, DataSourcePostProcessorExt.class})
public class BusDeviceTestApp {
    public static void main(String[] args) {
        SpringApplication.run(BusDeviceTestApp.class, args);
    }
}

主要实现以下功能:

package com.citylink.device;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.unitils.database.UnitilsDataSourceFactoryBean;

import javax.sql.DataSource;

@Component
public class DataSourcePostProcessorExt implements BeanPostProcessor {

    public DataSourcePostProcessorExt() {
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof DataSource) {
            try {
                return (new UnitilsDataSourceFactoryBean()).getObject();
            } catch (Exception var4) {
                throw new RuntimeException("replace database throw exception ,can not continue to process", var4);
            }
        } else {
            return bean;
        }
    }
}

测试类 - DeviceLogServiceTest

package com.citylink.device;

import com.citylink.device.pojo.Device;
import com.citylink.device.service.IDeviceService;
import com.citylink.security.common.msg.ObjectRestResponse;
import com.unitils.boot.util.UnitilsBootBlockJUnit4Cla***unner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.unitils.dbunit.annotation.DataSet;
import org.unitils.dbunit.annotation.ExpectedDataSet;
import org.unitils.spring.annotation.SpringBeanByType;

import java.util.List;

@RunWith(UnitilsBootBlockJUnit4Cla***unner.class)
@SpringBootTest(classes = BusDeviceTestApp.class)
@DataSet("dev_equipment_info.xlsx")
public class DeviceLogServiceTest {
    @SpringBeanByType
    private IDeviceService deviceService;

    @Test
    @ExpectedDataSet("expected_dev_equipment_info.xlsx")
    public void testSelectAll() {
        ObjectRestResponse orr = deviceService.selectAll();
        Assert.assertTrue(orr.isRel());
        ListdeviceList = (List) orr.getData();
        System.out.println("deviceList.size()=" + deviceList.size());
    }
}

               

标签:首次,DBUnit,database,拾遗,import,device,org,com,class
来源: https://blog.51cto.com/u_15181029/2733642