You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jeecg/test/spring/SpringTxTestCase.java

52 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package spring;
import jeecg.system.service.SystemService;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
/*
* @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
* transactionManager的默认取值是"transactionManager"
* defaultRollback的默认取值是true当然你也可以改成false。
* true表示测试不会对数据库造成污染,false的话当然就会改动到数据库中了。
* 在方法名上添加@Rollback(false)表示这个测试用例不需要回滚。
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring*.xml" })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class SpringTxTestCase extends AbstractJUnit4SpringContextTests {
protected SystemService systemService;
@Autowired
public void setSystemService(final SystemService systemService) {
this.systemService = systemService;
}
@Before
public void beforeClass() {
systemService.initAllTypeGroups();
}
public <T> T getBean(Class<T> type) {
return applicationContext.getBean(type);
}
public Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
protected ApplicationContext getContext() {
return applicationContext;
}
}