日期:2014-05-20  浏览次数:20830 次

关于Junit4测试没反应
现在做SSH整合的时候,在hibernate和spring整合过程中,用junit4测试,右键点击后run as->junit test,然后控制台无输出,求大家帮忙解决啊
下面是测试类的代码:
Java code

package com.ssh.service.impl;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ssh.bean.User;
import com.ssh.service.UserService;



public class UserServiceImplTest {
    public static UserService service;
    
    @BeforeClass
    public void setUpBeforeClass(){
        ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
        service = (UserServiceImpl)context.getBean("userServiceImpl");
    }
    
    @Test
    public void testSave(){
        User u = new User();
        u.setName("zhangsan");
        u.setPassword("123");
        service.addUser(u);
    }
}



userServiceImpl代码如下:
Java code

package com.ssh.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ssh.bean.User;
import com.ssh.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    private SessionFactory sessionFactory;
    
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    @Transactional(propagation=Propagation.SUPPORTS)
    public void addUser(User user) {
        getSessionFactory().getCurrentSession().persist(user);
    }

    public void delete(String name) {
        getSessionFactory().getCurrentSession()
                           .createQuery("delete User u where u.name=:name")
                           .setString("name", name)
                           .executeUpdate();
    }

    public User find(String name) {
        User user =(User)getSessionFactory().getCurrentSession().load(User.class,name);
        return user;
    }

    public List<User> list() {
        return getSessionFactory().getCurrentSession().createQuery("from User").list();
         
    }

    public void update(User user) {
        getSessionFactory().getCurrentSession()
                           .createQuery("update User u set u.name=:newName,u.password=:newPassword")
                           .setString("newName", user.getName())
                           .setString("newPassword", user.getPassword())
                           .executeUpdate();
    }

}


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"
  xmlns:tx="http://www.springframework.org/schema/tx"
  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
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">