Spring单元测试,SQL数据准备

  • 工具类代码

在测试中,一般的数据库数据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.*;

@Component
public class LineSyncTest extends SqlDataPrepareUtil{


	@Test
    @Rollback(true)
    public void should_return_XXX_when_given_xx(){

    }

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值