SSM框架测试dao或mapper层接口

思路:用test类加载spring配置文件来实现
1,jar包引入

<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
<properties>
		<spring.version>4.2.2.RELEASE</spring.version>
	</properties>
<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

2,创建测试父类

import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:application-config.xml","classpath*:servlet-config.xml"})
public class ObjectTest {

  protected final static Logger log = LoggerFactory.getLogger(ObjectTest.class);

}

@ContextConfiguration后面是spring配置文件位置
3,写具体测试子类

import com.uc.statge.mapper.CloudplatformDaoMapper;
import com.uc.statge.object.ObjectTest;
import com.uc.statge.pojo.Cloudplatform;
import com.uc.statge.pojo.User;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

public class CloudplatformDaoMapperTest extends ObjectTest {
    @Autowired
    CloudplatformService cfs;
    @Autowired
    CloudplatformDaoMapper cdfMapper;
    @Test
    public void findById() {
        User user =new User(3, "c@123.com" , "123123123" , " 2 5 " , 220000, 220100, 220105,
                "12323232333", 0 ,  0,  0);
        try {
            Map<String,Object> map = cfs.findById(user,1);
            for(Map.Entry<String,Object> entry:map.entrySet()){
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值