【发布时间】:2014-12-24 00:24:06
【问题描述】:
如果这是一个愚蠢的问题,请原谅我。我正在尝试通过做一个项目来学习 Spring MVC、Hibernate、Junit。我正在使用 MySQL 数据库。问题是我不了解如何测试我的项目 Junit 的 DAO 层或服务层。我已经轻松地测试了我的模型类。我搜索了 DAO & Service 层,但是我看到的教程让我更加困惑。它们都不符合我项目的模式。我知道用于测试过程的内存。还需要一些测试配置。某处文本上下文,某处使用了java配置类。现在,我实际上想知道我真正需要执行哪些步骤来测试这些层,如果您能告诉我每个步骤中必须执行的操作,将会很有帮助。我有几个 DAO、Service 和 Model 类。我从每一层都给了一门课。 我在 WEB-INF 中的 servlet-context.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:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Enable @Controller annotation support -->
<mvc:annotation-driven />
<!-- Map simple view name such as "test" into /WEB-INF/test.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
<context:component-scan base-package="com.mahin"/>
<!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
username root and blank password. Change below if it's not the case -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/webchatapp"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<!-- Hibernate Session Factory -->
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>com.mahin.models</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
我的模型课之一
package com.mahin.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="friends")
public class Friends {
@Id @GeneratedValue
private long friendid;
private long reqsender;
private long reqreceiver;
private int rank;
private boolean granted;
public long getFriendid() {
return friendid;
}
public void setFriendid(long friendid) {
this.friendid = friendid;
}
public long getReqsender() {
return reqsender;
}
public void setReqsender(long reqsender) {
this.reqsender = reqsender;
}
public long getReqreceiver() {
return reqreceiver;
}
public void setReqreceiver(long reqreceiver) {
this.reqreceiver = reqreceiver;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public boolean getGranted() {
return granted;
}
public void setGranted(boolean granted) {
this.granted = granted;
}
}
我的 DAO 课程之一
package com.mahin.daos;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.mahin.models.Friends;
@Repository
public class FriendsDAOimpl implements FriendsDAO{
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
@Override
public void addFriend(Friends friend) {
getCurrentSession().save(friend);
}
@Override
public void updateFriend(Friends friend) {
Friends friendToUpdate = getFriend(friend.getFriendid());
friendToUpdate.setGranted(friend.getGranted());
friendToUpdate.setRank(friend.getRank());
friendToUpdate.setReqreceiver(friend.getReqreceiver());
friendToUpdate.setReqsender(friend.getReqsender());
getCurrentSession().update(friendToUpdate);
}
@Override
public Friends getFriend(long friendid) {
Friends friend = (Friends) getCurrentSession().get(Friends.class, friendid);
return friend;
}
@Override
public void deleteFriend(long friendid) {
Friends friend = getFriend(friendid);
if (friend != null)
getCurrentSession().delete(friend);
}
@SuppressWarnings("unchecked")
@Override
public List<Friends> getFriends() {
return getCurrentSession().createQuery("from friends").list();
}
}
最后是我的服务类之一
package com.mahin.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mahin.daos.FriendsDAO;
import com.mahin.models.Friends;
@Service
@Transactional
public class FriendsServiceimpl implements FriendsService{
@Autowired
private FriendsDAO friendsDAO;
@Override
public void addFriend(Friends friend) {
friendsDAO.addFriend(friend);
}
@Override
public void updateFriend(Friends friend) {
friendsDAO.updateFriend(friend);
}
@Override
public Friends getFriend(long friendid) {
return friendsDAO.getFriend(friendid);
}
@Override
public void deleteFriend(long friendid) {
friendsDAO.deleteFriend(friendid);
}
@Override
public List<Friends> getFriends() {
return friendsDAO.getFriends();
}
}
【问题讨论】:
标签: java spring hibernate spring-mvc junit