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
//defaultRollback = true 则测试完毕自动回滚,无法看到数据库是否插入数据,测试环境应该设置为false,否则无法确定是否成功提交事务
//defaultRollback = false
@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);
}
}
}