junit4+spring单元测试

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.hamcrest.Matchers.*;

import static org.junit.Assert.*;


/**
 * Created by yanlei on 2014/7/31.
 Spring+Junit4 单元测试
 类级别的Annotation:
 @RunWith(SpringJUnit4ClassRunner.class)  --指明用spring+junit4进行单元测试
 @ContextConfiguration(value = {"classpath*:config/spring/base/component*.xml"})  --指明spring配置文件,多个配置文件用逗号分隔,在类中就可以使用@Resource 或@AutoWire 引用spring配置的Bean
 @FixMethodOrder(MethodSorters.NAME_ASCENDING)--@Test 标注的测试方法,按方法名排序结果顺序(由小到大)进行测试

 属性级别的Annotation:
 @Resource(name="personMapper") ---spring注入bean

 方法级别的Annotation:
 @BeforeClass --方法必须为static void 最先执行且只执行一次
 @AfterClass  --方法必须为static void 最后执行且只执行一次
 @Before   --每个标注@Test的测试方法之前执行,执行多次,最先执行那次是在@BeforeClass之后
 @Test  --标注该方法为测试方法在,可以标注多个方法,即多个方法都是测试方法,多个测试方法测试顺序由FixMethodOrder决定,每个测试方法在 @Before之后执行
    参数 @Test(expected = Exception.class) 方法抛出指定的异常,则测试通过
         @Test(timeout=5000) 方法执行时间超过5秒,则测试不通过
 @Ignore 忽略的测试方法

执行顺序:@BeforeClass->@Before->@Test(方法1)->@After->@Before->@Test(方法2)->@After....@Before->@Test(方法N)->@After->@AfterClass
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath*:config/spring/base/component*.xml"})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PersonTest {

    @Resource(name="personMapper")//spring注入bean
    private PersonMapper personMapper;


   private static  Person person = null;
    @BeforeClass
    public static void before(){
        person = new Person();
        person.setBirthday(new Date());
        String id= AppStringUtils.getUUID();
        person.setId(id);

        person.setIdentityNumber("2102302930");
        person.setName("yanlei");
        person.setSex("男");
    }
    @Test
    public void test1DeleteByExamplee(){
        PersonExample  personExample = new PersonExample();
        personExample.createCriteria().andNameEqualTo("yanlei");
        this.personMapper.deleteByExample(personExample);
    }
    @Test

    public void test3Insert(){

        int insertNum = this.personMapper.insert(person);
        assertThat(1,equalTo(insertNum));

    }
    @Test
    public void test5CountByExample(){
       List<Date> birthdayList= new ArrayList();
        birthdayList.add(AppDateUtils.parseString("20000101"));
        birthdayList.add(AppDateUtils.parseString("20010101"));
        birthdayList.add(new Date());
        PersonExample  personExample = new PersonExample();
        personExample.createCriteria()
                .andBirthdayBetween(AppDateUtils.parseString("20000101"),AppDateUtils.parseString("2020101"))
                .andBirthdayEqualTo(new Date())
                .andBirthdayGreaterThan(AppDateUtils.parseString("20140101"))
                .andBirthdayIn(birthdayList)
                .andBirthdayIsNotNull()
                .andBirthdayLessThan(AppDateUtils.parseString("2020101"))
                .andBirthdayLessThanOrEqualTo(AppDateUtils.parseString("2020101"))
                .andBirthdayNotBetween(AppDateUtils.parseString("2020101"),AppDateUtils.parseString("2021101"));

       int countNum  =  this.personMapper.countByExample(personExample);
       assertThat(countNum,equalTo(1));

    }
    @Test
    public void test9delete(){

            int deleteNum = this.personMapper.deleteByPrimaryKey(person.getId());
            assertThat(deleteNum, equalTo(1));

    }
}

 

你可能感兴趣的:(spring)