spring4.x使用junit4,事务自动回滚

阅读更多

 

使用事务需要继承AbstractTransactionalJUnit4SpringContextTests

@Rollback事务自动回滚

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.urthink.core.authority.entity.User;
import com.urthink.core.base.JdbcTemplateDao;

@RunWith(SpringJUnit4ClassRunner.class) // 整合
@Rollback // 事务自动回滚
@ContextConfiguration(locations = {"classpath:spring-context.xml"}) // 加载配置
public class JdbcTemplateTest extends AbstractTransactionalJUnit4SpringContextTests {
	@Autowired
	private JdbcTemplateDao jdbcTemplateDao;

	@Test
	public void test() {
		List users = jdbcTemplateDao.queryList();
		System.out.println(users.size());
	}
	
	@Test
	public void insert() {
		User user = new User();
		user.setOrganizationId(1000);
		user.setUsername("test1");
		user.setPassword("1");
		user.setEnable(true);
		jdbcTemplateDao.insert(user);
	}
	
	@Test
	public void update() {
		User user = jdbcTemplateDao.queryById(16);
		user.setOrganizationId(1000);
		user.setUsername("test2");
		user.setPassword("2");
		user.setEnable(true);
		jdbcTemplateDao.update(user);
	}
	
	@Test
	public void delete() {
		jdbcTemplateDao.delete(16);
	}
}

 

你可能感兴趣的:(spring,junit,事务,回滚)