junit-单元测试

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");
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值