zoukankan      html  css  js  c++  java
  • spring测试框架的使用

    junit的使用

    1、加入 junit jar包

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    2、标注@Test注解、调用测试框架方法、调用 httpclient 方法

    Assert.assertNotNull(user);
    Assert.assertNull(user.getId());
    Assert.assertTrue(userList.size() > 0);
    Assert.assertEquals("admin", user.getUserName()); 

    junit、spring-test 的使用

    1、加入 junit 及 spring-test jar包

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.14.RELEASE</version>
    </dependency>
    View Code

    2、在测试类上添加注解,并指定spring配置文件的classpath

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:spring/spring-context.xml") //@ContextConfiguration(classes=xxx.class)
        public class TestJedis {
    
    }

    3、标注@Test注解、调用测试框架方法、调用 httpclient 方法(同上)


    junit、pring-test 的网络使用示例

    @ContextConfiguration(locations = "classpath:conf/applicationContext.xml")
    @RunWith(SpringJUnit4ClassRunner.class)
    @Transactional
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
    public abstract class AbstractTestCase extends
            AbstractTransactionalDataSourceSpringContextTests {
     
    }
    将配置项都写在父类中,测试子类继承该AbstractTestCase类,测试的时候,不会导致数据库数据污染,因为继承AbstractTransactionalDataSourceSpringContextTests每次数据库操作完成之后都会回滚。
  • 相关阅读:
    【HDU
    写个shell脚本依次运行每个程序半小时
    Windows10 + Visual Studio 2017环境为C++工程安装使用ZMQ
    【UVALive
    【Gym
    【最短路算法】Dijkstra+heap和SPFA的区别
    【Gym 100812C】Story of Princess (走完图所有边)
    【C++】VS2015/VS2017连接Mysql数据库教程
    博客园设置Google-code-prettify渲染代码高亮
    【QML与C++混合编程】用QVariantList传递数组类型成员
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/9945353.html
Copyright © 2011-2022 走看看