上一篇就针对spring开发时的2种方式给予Junit测试的一个简单说明。这篇为了给新人们一个明确的说明,我就直接符上代码,供大家参考;
1.实体类:
package com.tta.model; public class User implements java.io.Serializable { private Integer id; private String name; private Integer age; private String address; public User() { } public User(Integer age, String address) { this.age = age; this.address = address; } public User(String name, Integer age, String address) { this.name = name; this.age = age; this.address = address; } public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } }
实体orm映射文件:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.tta.model.User" table="tb_model_user" > <id name="id" type="integer"> <column name="id" /> <generator class="native"></generator> </id> <property name="name" type="string"> <column name="name" length="20" /> </property> <property name="age" type="integer"> <column name="age" not-null="true" /> </property> <property name="address" type="string"> <column name="address" length="50" not-null="true" /> </property> </class> </hibernate-mapping>
2.服务层代码:
package com.tta.svc; import java.util.List; import com.tta.model.User; public interface IUserService { void saveUser(User u); void deleteUser(User u); void updateUser(User u); User findUserById(int userId); List<User> findUsers(); }
package com.tta.svc.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.tta.dao.IUserDao; import com.tta.model.User; import com.tta.svc.IUserService; @Service("userService") @Transactional public class UserService implements IUserService { private IUserDao useDao; @Override public void deleteUser(User u) { useDao.deleteUser(u); } @Override public User findUserById(int userId) { return useDao.findUserById(userId); } @Override public List<User> findUsers() { return useDao.findUsers(); } @Override public void saveUser(User u) { useDao.saveUser(u); } @Override public void updateUser(User u) { useDao.updateUser(u); } public void setUseDao(IUserDao useDao) { this.useDao = useDao; } }
3.数据操作层:
package com.tta.dao; import java.util.List; import com.tta.model.User; public interface IUserDao { void saveUser(User u); void deleteUser(User u); void updateUser(User u); User findUserById(int userId); List<User> findUsers(); }
package com.tta.dao.impl; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.tta.dao.IUserDao; import com.tta.model.User; @Repository("userDao") public class UserDao extends HibernateDaoSupport implements IUserDao { @Autowired public void setSuperSessionFactory(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } @Override public void deleteUser(User u) { getHibernateTemplate().delete(u); } @Override public User findUserById(int userId) { User u = new User(); u.setId(userId); return (User) getHibernateTemplate().findByExample(u).get(0); } @Override public List<User> findUsers() { //Model User // return getHibernateTemplate().find("from User"); //View User return getHibernateTemplate().find("select new User(age,address) from User"); } @Override public void saveUser(User u) { getHibernateTemplate().save(u); } @Override public void updateUser(User u) { getHibernateTemplate().merge(u); } }
4.spring配置文件:
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.tta.*" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mbpa"></property> <property name="username" value="root"></property> <property name="password" value="sainwind"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"></ref> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/tta/model/User.hbm.xml</value> </list> </property> </bean> </beans>
最后,贴上我们的测试类:
package com.tta.svc.test; import java.util.List; import org.junit.Test; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tta.model.User; import com.tta.svc.IUserService; import com.tta.svc.impl.UserService; public class UserTest { private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); private static AutowireCapableBeanFactory autoBeanFactory = applicationContext.getAutowireCapableBeanFactory(); private static IUserService svc = (UserService)autoBeanFactory.getBean("userService"); // @Test public void save(){ User u = new User(22,"zhangsan"); svc.saveUser(u); } // @Test public void delete(){ svc.deleteUser(svc.findUserById(1)); } // @Test public void update(){ User u = svc.findUserById(1); u.setAddress("上海市徐汇区零陵路123号"); u.setAge(35); u.setName("赵冰"); svc.updateUser(u); } @Test public void getUsers(){ List<User> users = svc.findUsers(); for(User u : users){ System.out.println("name : "+u.getName()+" address:"+u.getAddress()); } } }
有问题可以一起探讨啊。希望大家多多指点。
相关推荐
免费了,spring4+JUnit简单测试,注解组件扫描和xml显示组件扫描,我自己都忘了里面有什么了。
NULL 博文链接:https://zkf60553.iteye.com/blog/1604723
基于Spring的JUnit4单元测试
基于Spring的JUnit4单元测试
该资源包含了spring整合单元测试Junit开发包spring-test-4.2.4.RELEASE.jar
junit测试 test txt file
struts2_spring3.0_Junit4.7_Maven2.2.1_整合运行说明_培训
spring3 junit 测试 + word文档,里面的例子能直接运行.
junit测试测试junit测试测试junit测试测试junit测试测试
通过Spring集成单元测试,以后测试service层内容时,就不用getBean()来获得Service层对象了
struts-junit spring-mock spring-test junit等的javadoc.jar格式的API文档,直接导入Eclipse/MyEclipse/Netbeans等IDE即可实现快速API查询。 包含以下文件: File name -------------------------------------- ...
Spring3+junit的单元测试的代码示例。
spring4+junit4.8 +多线程TheadTool
使用 tk-mybatis 的 demo 测试了 Spring 的事务 包含 @Transactional 注解所有的功能测试。
Junit,测试Junit,测试案例使用
junit针对spring mvc三层测试,通过测试运行,可直接下载运行!
一个使用junit的标注测试,可以免于写过的的每个方法都使用setup
Junit测试.mp4
Myeclipse中的JUnit测试详细
Junit测试jar包