首页 > 代码库 > spring junit jpa transaction

spring junit jpa transaction

package com.fengshu.gotian.applicationImpl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

import com.fengshu.gotian.application.UserApplication;
import com.fengshu.gotian.core.User;

@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:META-INF/spring/ApplicationContext.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
		TransactionalTestExecutionListener.class })
public class UserApplicationImplTest extends AbstractJUnit4SpringContextTests {
	private Logger logger = Logger.getLogger(this.getClass());

	@Resource(name = "userApplication")
	private UserApplication userApplication;

	@Test
	public void testSpring() {
		logger.info("test begin");
		List<User> users = userApplication.find("from User");
		for (User user : users) {
			userApplication.delete(user);
		}

	}

}



spring junit jpa transaction