junit-單元測試

ZHOU_VIP發表於2017-03-22

1.pom.xml中匯入jar


2.


3.


4.


5.


6.


7.NewUserMapperTest.java


package cn.itcast.mybatis.mapper;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.github.abel533.entity.Example;

import cn.itcast.mybatis.pojo.User;

public class NewUserMapperTest {

    private NewUserMapper newUserMapper;

    @Before
    public void setUp() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "classpath:spring/applicationContext*.xml");
        this.newUserMapper = applicationContext.getBean(NewUserMapper.class);
    }

    @Test
    public void testSelectOne() {
        User record = new User();
        // 設定查詢條件
        record.setuserName("zhangsan");
        record.setPassword("123456");
        User user = this.newUserMapper.selectOne(record);
        System.out.println(user);
    }

    @Test
    public void testSelect() {
        User record = new User();
        // 設定查詢條件
        record.setuserName("zhangsan");
        List<User> list = this.newUserMapper.select(record);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testSelectCount() {
        System.out.println(this.newUserMapper.selectCount(null));
    }

    @Test
    public void testSelectByPrimaryKey() {
        User user = this.newUserMapper.selectByPrimaryKey("1");
        System.out.println(user);
    }

    @Test
    public void testInsert() {
        User record = new User();
        // 設定查詢條件
        record.setuserName("test_username_3");
        //record.setAge(20);
        //record.setBirthday(new Date());
        record.setCreated(new Date());
        //record.setName("test_name_1");
        //record.setPassword("123456");
        record.setSex(1);
        record.setUpdated(new Date());
        //使用所有的欄位作為插入語句的欄位
        int count = this.newUserMapper.insert(record);
        System.out.println(count);
        System.out.println(record.getId());
    }

    @Test
    public void testInsertSelective() {
        User record = new User();
        // 設定查詢條件
        record.setuserName("test_username_2");
        //record.setAge(20);
       // record.setBirthday(new Date());
        record.setCreated(new Date());
       // record.setName("test_name_1");
       // record.setPassword("123456");
        record.setSex(1);
        record.setUpdated(new Date());
        //將不為null的欄位作為插入語句的欄位
        int count = this.newUserMapper.insertSelective(record);
        System.out.println(count);
        System.out.println(record.getId());
    }

    @Test
    public void testDelete() {
//        this.newUserMapper.delete(null);
    }

    @Test
    public void testDeleteByPrimaryKey() {
        System.out.println(this.newUserMapper.deleteByPrimaryKey(9L));
    }

    @Test
    public void testUpdateByPrimaryKey() {
        fail("Not yet implemented");
    }

    @Test
    public void testUpdateByPrimaryKeySelective() {
        User record = new User();
        record.setId(1L);
        record.setAge(20);
        this.newUserMapper.updateByPrimaryKeySelective(record);
    }

    @Test
    public void testSelectCountByExample() {
        fail("Not yet implemented");
    }

    @Test
    public void testDeleteByExample() {
        fail("Not yet implemented");
    }

    @Test
    public void testSelectByExample() {
        Example example = new Example(User.class);
        List<Object> values = new ArrayList<Object>();
        values.add(1L);
        values.add(2L);
        values.add(3L);
        example.createCriteria().andEqualTo("id", values);
        List<User> list = this.newUserMapper.selectByExample(example);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testUpdateByExampleSelective() {
        fail("Not yet implemented");
    }

    @Test
    public void testUpdateByExample() {
        fail("Not yet implemented");
    }

}