在junit中使用open session in view

废话不多说直接上代码:

jpa的

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
import org.springframework.orm.jpa.EntityManagerHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.Date;
import java.util.Iterator;

/**
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-test.xml"})
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Autowired
    /**
     * 需要声明这个对象
     */
    private EntityManagerFactory entityManagerFactory;

    @Before
    /**
     * 在测试执行之前把entityManager加入到事务同步管理器当中
     */
    public void before() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        TransactionSynchronizationManager.bindResource(entityManagerFactory , new EntityManagerHolder(entityManager));

    }

    @After
    /**
     * 在测试执行完毕之后,关闭entityManager,并且接触绑定
     */
    public void after() {
        EntityManagerHolder holder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(entityManagerFactory);
        EntityManagerFactoryUtils.closeEntityManager(holder.getEntityManager());
        TransactionSynchronizationManager.unbindResource(entityManagerFactory);
    }

    @Test
    public void testSave() {
        Book book = new Book();

        book.setIsbn("123-123-11");
        book.setPubdate(new Date());
        book.setRegdate(new Date());
        book.setTitle("test book title");

        bookService.save(book, 5L);

    }

    @Test
    public void testFind() {
        Iterable<Book> bookIterable = bookService.list();
        Iterator<Book> bookIterator = bookIterable.iterator();

        while(bookIterator.hasNext()) {
            Book book = bookIterator.next();
            System.out.println(book.getOwner());
        }
    }
}

下面是hibernate的

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })  
public class OpenSessionInViewTest extends AbstractTransactionalSpringContextTests {  
  
    @Resource(name="hibernateSessionFactory")  
    private SessionFactory sessionFactory;  
         
    @Before  
    public void init() {  
        Session s = sessionFactory.openSession();  
        TransactionSynchronizationManager.bindResource(sessionFactory,    
                new SessionHolder(s));    
    }  
  
    @Test  
    public void testQuery() {  
        SessionHolder holder = (SessionHolder)TransactionSynchronizationManager.getResource(sessionFactory);  
        Session session = holder.getSession();  
        User user= (User)session.get(User.class, "abc0003");  
        System.out.println(user.getId());  
        Set<Book> books= user.getBooks();  
        for (Book book: books) {  
            System.out.println(book.getId());  
        }  
    }  
  
    @After  
    public void close() {  
        SessionHolder holder = (SessionHolder)TransactionSynchronizationManager.getResource(sessionFactory);  
        SessionFactoryUtils.closeSession(holder.getSession());  
        TransactionSynchronizationManager.unbindResource(sessionFactory);    
    }  
}
 

相关推荐