Spring添加Junit4支持

Spring框架提供了Test模块,极大简化了项目开发的测试。下面总结一下Spring中Junit的使用:

相关jar包下载地址:http://pan.baidu.com/s/1qY55eP2

 

1.引入Spring核心容器模块,让Spring容器创建管理我们的Bean

    

  

2.引入Spring的Test模块

   

3.编码测试

 

User的设计

package com.heima_jdbctemplate_c3p0;
/**
 * 标准JavaBean
 * @author GGR
 *
 */
public class User {
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
    private Integer _id;//用户id
    private String username;//用户名
    private String userpwd;//用户密码

    public User(){}
    public User(String username,String userpwd){
        this.username = username;
        this.setUserpwd(userpwd);
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpwd() {
        return userpwd;
    }
    public void setUserpwd(String userpwd) {
        this.userpwd = userpwd;
    }
    public Integer get_id() {
        return _id;
    }
    public void set_id(Integer _id) {
        this._id = _id;
    }
}
View Code

 

UserDao设计

package com.heima_jdbctemplate_c3p0;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.heima_jdbctemplate_dbcp.User;

@Repository("userdao")
public class UserDao{
    @Resource(name="jdbctemplate")
    private JdbcTemplate jdbcTemplate;
    public List<User> getAll(){
        return jdbcTemplate.query("select* from t_user",ParameterizedBeanPropertyRowMapper.newInstance(User.class));        
    }
}
View Code

 

jdbc配置文件jdbcinfo.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/spring_database
jdbc.user=root
jdbc.password=362427gg
View Code

 

mysql数据库

/*
Navicat MySQL Data Transfer

Source Server         : gg
Source Host           : localhost:3306
Source Database       : spring_database

Target Server Type    : MYSQL
File Encoding         : 65001

Date: 2017-03-08 14:27:35
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `userpwd` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', 'jake', '123456');
INSERT INTO `t_user` VALUES ('2', 'rose', '123456789');
INSERT INTO `t_user` VALUES ('3', 'tom', '999');
View Code

 

bean.xml配置  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据源    -->
<context:property-placeholder location="classpath:com/heima_jdbctemplate_c3p0/jdbcInfo.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driverClass}"></property>
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 扫描含有注解的包 -->
<context:component-scan base-package="com.heima_jdbctemplate_c3p0"></context:component-scan>
<!-- 配置jdbctemplate实例 -->
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
View Code

 

测试代码   

package com.heima_jdbctemplate_c3p0;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/heima_jdbctemplate_c3p0/bean.xml")
public class Testdamo {
    @Autowired
    @Qualifier("userdao")
    private UserDao userdao;
    
    @Test
    public void demo(){
        System.out.println(userdao.getAll());
    }
}
View Code

 

 

  

 

posted on 2017-03-08 23:10  昨天的你,现在的未来  阅读(1871)  评论(0编辑  收藏  举报