请原谅我这是一个愚蠢的问题。我正在尝试通过做一个项目来学习Spring MVC,Hibernate,Junit。我正在使用MySQL数据库。问题是我不了解如何测试我的项目Junit的DAO层或服务层。我已经轻松测试了我的模型类。我搜索了DAO&服务层,但我看到的教程,让我更加困惑。它们都没有实际匹配我的项目模式。我知道内存用于测试过程。还需要一些测试配置。某处文本上下文,某处使用了java配置类。现在,我实际上想知道我真正需要做什么步骤来逐一测试这些图层,如果你能告诉我每一步要做什么都会有所帮助。我有几个DAO,Service和Model类。我从每一层给了一个班级。 我的servlet-context.xml文件位于WEB-INF:
中 <?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();
}
}
答案 0 :(得分:3)
您有两种选择:
单元测试:测试方法是否按预期单独工作。这是JUnit的唯一目的。对于单元测试,您不能连接到数据库之类的外部资源。也就是说,您必须模拟您的数据源提供程序以及您正在测试的类外部的任何其他组件。您可以使用PowerMock,EasyMock或Mockito等模拟框架来模拟方法和类。
集成测试:测试方法是否在集成环境中按预期工作。在这里,您可以测试该类是否可以与其他外部资源(如数据库连接)进行交互,并执行与这些组件集成的功能。
对于Spring项目,有Spring Test框架,它为JUnit测试类提供了进行单元或集成测试的附加功能。这是在您发布的Dao类上执行集成测试的基本示例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("path/to/your/spring-config.xml")
public class MyTest {
@Autowired
FriendsDAO friendsDao;
@Test
public void testAddFriend() {
final int rank = 1;
Friends friend = new Friends();
friend.setRank(rank);
friendsDao.addFriend(friend);
final long friendId = friend.getId();
Friends insertedFriend = friendsDao.getFriend(friendId);
Assert.assertEquals(insertedFriend.getRank(), friend.getRank());
//assert if all necessary fields are equal
//or assert if both are equals in case your Friends class implements equals method
}
}