正确写法应该如下:
测试无错误的环境:spring4.2.5 junit4.10
参考:http://www.ibm.com/developerworks/cn/java/j-lo-springunitest/
http://my.oschina.net/dlpinghailinfeng/blog/336694
特别感谢以上2篇文章,尤其IBM的,果然是顶级,其他人写的基本错误,看到IBM后,尤为开心,秒杀解决问题。
其他类继承SpringTest 即可。
import org.junit.runner.RunWith;
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.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations="classpath: /com/config/spring-core.xml")
@ContextConfiguration("/com/config/spring-core.xml")
@Transactional
public class SpringTest extends AbstractJUnit4SpringContextTests {
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;
}
}