博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring单元测试,SQL数据准备
阅读量:6917 次
发布时间:2019-06-27

本文共 3224 字,大约阅读时间需要 10 分钟。

hot3.png

  • 工具类代码

在测试中,一般的数据库数据Mock是不可少的。在前文的基础上,我们写读取脚本把数据写入数据库的工具类。

import org.junit.After;import org.junit.Before;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.sql.DataSource;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:testApplicationContext.xml"})public class SqlDataPrepareUtil {    @Autowired    DataSource dataSource;    SimpleJdbcTemplate template;    private final Logger logger = LoggerFactory.getLogger(SqlDataPrepareUtil.class);    @Before    public void setUp() throws IOException {        template = new SimpleJdbcTemplate(dataSource);        String content = sqlForThisTest();        if (content.equals("")) {            return;        }        String[] sqlLines = content.split(";");        for (int i = 0; i < sqlLines.length; i++) {            String sql = sqlLines[i];            if (0 == sql.trim().length()) {                continue;            }            template.update(sql);            if (logger.isDebugEnabled()) {                logger.debug(sql);            }        }    }    private String sqlForThisTest() throws IOException {        String sqlName = getClass().getSimpleName() + ".sql";        InputStream stream = getClass().getResourceAsStream(sqlName);        if (stream == null) {            return "";        }        BufferedReader reader = new BufferedReader(                new InputStreamReader(stream, "UTF-8"));        StringBuilder buffer = new StringBuilder();        try {            String line = null;            while ((line = reader.readLine()) != null) {                buffer.append(line);            }        } finally {            reader.close();        }        return buffer.toString();    }}

  • 工具类使用

脚本存放的地方和测试类包名路径一致,测试类的写法如下:

import org.jboss.resteasy.mock.MockHttpRequest;import org.jboss.resteasy.mock.MockHttpResponse;import org.junit.Assert;import org.junit.Ignore;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.test.annotation.Rollback;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.transaction.annotation.Transactional;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response.Status;import java.io.UnsupportedEncodingException;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.List;import static com.natpryce.makeiteasy.MakeItEasy.*;@Componentpublic class LineSyncTest extends SqlDataPrepareUtil{	@Test    @Rollback(true)    public void should_return_XXX_when_given_xx(){    }}

转载于:https://my.oschina.net/darkness/blog/364065

你可能感兴趣的文章
用SPK技术分析,泰坦尼克号沉船之后哪些人活下来了
查看>>
多线程(八)---单例模式并发访问
查看>>
堵车预测神器诞生 浙江已用来治疗高速顽疾
查看>>
Android Span的简单使用
查看>>
centos7 挂载数据盘
查看>>
Prometheus+Grafana监控MySQL
查看>>
cas server 实现LDAP、数据库认证
查看>>
Java九九乘法表
查看>>
聊聊:Linux分区的那些方案
查看>>
大数据时代快速SQL引擎-Impala
查看>>
记一次IO异常捕获的过程
查看>>
控制器中如何设计MMU--虚拟内存管理机制
查看>>
Flutte部件目录-基本部件(一)
查看>>
AngularDart Material Design 按钮
查看>>
关于切换页面时自动滚动到有RecyclerView的位置的解决方法
查看>>
&&和&
查看>>
POI cell的宽度自适应
查看>>
检查ipa包是否包含手机的方法
查看>>
vim命令用法
查看>>
python复习2
查看>>