经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » JUnit » 查看文章
ssh(spring,struts2,hibernate)框架整合junit4
来源:cnblogs  作者:冰湖一角  时间:2018/9/25 19:08:59  对本文有异议

step1:导入必须的包,如果是maven项目,直接在pom.xml文件里加入以下依赖包:

    <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>2.5.5</version>  
            <scope>test</scope>  
        </dependency> 
        <!-- 测试action-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-junit-plugin</artifactId>
            <version>2.3.15.1</version>
            <scope>test</scope>
        </dependency>
        <!-- 模拟http请求 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>test</scope>
        </dependency>

step2:在src/main/java下建测试类

step2.1:测试action

//测试action的类需要继承StrutsSpringTestCasepublic class JunitTrackActionTest extends StrutsSpringTestCase{

    @Testpublic void test() {//设置 请求参数request.setParameter("startTime", "2018-09-11 00:00:00");
        request.setParameter("endTime", "2018-09-11 12:00:00");
        request.setParameter("queryReliability", "99");try {//括号里的是前台发送的url连接String result = executeAction("/trackAction!getTrackByParams.action");
            System.out.println(result);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
}

step2.2:测试service

public class JunitTrackServiceTest {private String startTime;private String endTime;private String queryReliability;private TrackService trackService;

    @Beforepublic void init() {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
        trackService = (TrackService) classPathXmlApplicationContext.getBean("trackService");

        startTime = "2018-09-11 00:00:00";
        endTime = "2018-09-11 12:00:00";
        queryReliability = "99";
    }
    
    @Testpublic void test() {
        List<Track> list = trackService.getTrackByParams(startTime, endTime, Integer.parseInt(queryReliability));
        
        System.out.println(list.get(0).getCaptureTime());
    }

}

step2.3:测试dao

public class JunitTrackDaoTest {private String startTime;private String endTime;private String queryReliability;private TrackDAO trackDAO;

    @Beforepublic void init() {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
        trackDAO = (TrackDAO) classPathXmlApplicationContext.getBean("trackDAO");

        startTime = "2018-09-11 00:00:00";
        endTime = "2018-09-11 12:00:00";
        queryReliability = "99";
    }
    
    @Testpublic void test() {
        List<Track> list = trackDAO.getTrackByParams(startTime, endTime, Integer.parseInt(queryReliability));        ///JSONObject json = JSONObject.fromObject(list.get(0));System.out.println(list.get(0).getCaptureTime());

    }

}

最后,点击鼠标右键--》run as--->JUnit Test,即可

 友情链接: NPS