spring4+junit4单元测试

1.jar的支持。

  1.1 junit4    spring-test   spring-context

2.写法案例

 

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

import com.hcsj.service.Sgs;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"../../applicationContext.xml"})
public class Test1 {

    @Autowired
    private Sgs sgsImpl;
    
    @org.junit.Test
    public void Test(){
        sgsImpl.getXxxx("9900000001");
    }
}

上面的locations={"../../applicationContext.xml"}路径一定要找对,否则不能使用,因项目不同路径不同。

注解解释:

  • @RunWith:用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入,spring提供org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为Junit测试环境
  • @ContextConfiguration:用于指定spring配置环境
  • @TransactionConfiguration:用于配置事务
  • @Transactional:表示所有类下所有方法都使用事务

 

posted @ 2017-03-02 10:21  园中菜鸟  阅读(121)  评论(0)    收藏  举报