JUnit 单元测试的数据源配置

 

1. 配置用于本地测试时的XML文件,模拟tomcat的数据库jndi

<?xml version="1.0" encoding="UTF-8"?>  
<beans:beans 
xmlns:beans="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  

<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<beans:property name="driverClassName" value="org.postgresql.Driver" />  
<beans:property name="url" value="jdbc:postgresql://IP:PORS/DBNAME" />  
<beans:property name="username" value="user" />  
<beans:property name="password" value="password" />  
</beans:bean>  
   
</beans:beans>

2. JunitBaseTest

import javax.sql.DataSource;

import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= { 
        "file:WebContent/WEB-INF/applicationContext-*.xml",
        "file:WebContent/WEB-INF/mvc-dispatcher-servlet.xml"
    })
public class JunitBaseTest {
    
    @BeforeClass
    public static void beforeClass() throws Exception {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("file:WebContent/WEB-INF/jndiTest.xml");
        DataSource ds = (DataSource) app.getBean("dataSource");
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        builder.bind("jndiName", ds);
        builder.activate();
        app.close();
    }
}

3. TingTest  单元测试

import java.io.IOException;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class TingTest extends JunitBaseTest {

    private static final Logger logger = Logger.getLogger(TingTest.class);

   
    @Autowired
    TingMapper tingMapper;

    @Test
    public void tingTest() throws IOException, InterruptedException {
        try {
            String res =
                    "{\"error_code\":0,\"reason\":\"ok\",\"result\":{\"path\":[{\"type\":\"name\",\"value\":\"李彦宏\"},{\"type\":\"percent\",\"value\":\"99.50%\"},{\"type\":\"name\",\"value\":\"北京百度网讯科技有限公司\"}],\"structure\":{\"id\":\"22822\",\"name\":\"北京百度网讯科技有限公司\",\"type\":\"C\",\"amount\":null,\"percent\":null,\"sh_type\":null,\"children\":[{\"id\":\"1984012283\",\"name\":\"李彦宏\",\"type\":\"H\",\"amount\":\"2142.36\",\"percent\":\"99.50%\",\"sh_type\":\"工商股东\",\"children\":[],\"regCapital\":null,\"parentName\":null,\"actualHolding\":true},{\"id\":\"1839080315\",\"name\":\"向海龙\",\"type\":\"H\",\"amount\":\"1085.64\",\"percent\":\"0.50%\",\"sh_type\":\"工商股东\",\"children\":[],\"regCapital\":null,\"parentName\":null,\"actualHolding\":false}],\"regCapital\":\"217128\",\"parentName\":null,\"actualHolding\":true}}}";
            JSONObject json = JSON.parseObject(res);
            int errorCode = json.getIntValue("error_code");
            if (errorCode == 0) {
                JSONObject result = json.getJSONObject("result");
                JSONArray path = result.getJSONArray("path");
                tingMapper.savePath(relationId, path);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值