Spring Test + JUnit

余生颓废 提交于 2019-11-28 19:03:10

1. pom.xml

 <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
 </dependency>

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.2.5.RELEASE</version>
 </dependency>

2. 定义一个测试基类:

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

@RunWith(SpringJUnit4ClassRunner.class)//表示整合JUnit4进行测试
@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载spring配置文件
public class BaseJunit4Test {

}

3. 测试类

package com.zerotest.lotus;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.sltas.application.entrance.model.EntranceParameter;
import com.sltas.entrance.dao.EntranceParameterMapper;


public class SpringRedisTest extends BaseJunit4Test {

    @Autowired
    private EntranceParameterMapper entranceParameterMapper;
    
    @Test
    public void test01() {
    	EntranceParameter epm01 = new EntranceParameter();
    	epm01.setCategory("aaaaaa");
    	epm01.setMerchId(111261);
    	epm01.setParamKey("aaa - key");
    	epm01.setParamValue("aaa = value");
    	entranceParameterMapper.insert(epm01);
    	/*
    	EntranceParameter epm = entranceParameterMapper.selectByPrimaryKey(5);
        System.out.println(epm.getParamKey());
        System.out.println(epm.getCategory());
        System.out.println(epm.getParamValue());
        */

    }

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!