Spring Test + JUnit

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());
        */

    }

}
相關文章
相關標籤/搜索