spring 单元测试

来源:互联网 发布:深入理解大数据 黄宜华 编辑:程序博客网 时间:2024/05/14 09:45

在spring mvc 框架下。

1.引入junit4  和 spring-test 的架包 。

<dependency>  

<groupId>org.springframework</groupId>  

  <artifactId>spring-test</artifactId>  

<version>4.1.1.RELEASE</version>

  </dependency>

2.1 使用自动注入:

@ContextConfiguration(locations = {"classpath:spring-jdbc.xml","classpath:applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)

public class MyTest extends AbstractTransactionalJUnit4SpringContextTests{

   @Autowired
    AmmsDomainService ammsService;

          @Test
    public void Ammstest()
    {
       CacheMerchant cacheMerchant= ammsService.queryCacheMerchantAllByPosMerchant("827000058130091", "00007113");
       System.out.println("通过posMid, TID 查询===="+cacheMerchant);

    }

}

2.2手动加载spring的配置文件,并启动spring容器
   

public class MyTest extends AbstractTransactionalJUnit4SpringContextTests{

 


    public void Ammstest()
    {   

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

context.start();

CacheMerchant cacheMerchant= ammsService.queryCacheMerchantAllByPosMerchant("827000058130091", "00007113");
       System.out.println("通过posMid, TID 查询===="+cacheMerchant);

    }

}



3.run as juniTest 就可以啦


0 0
原创粉丝点击