开发单元测试,测试spring环境

标签: spring testng
33人阅读 评论(0) 收藏 举报
分类:

前言

要想测试spring开发环境,在测试过程中自然要启动容器applicationContext.xml,在下文会介绍搭建一个利用testng进行单元测试的简单方法。

编码环境

  • Intellij IDEA 2018.1
  • Maven 3.3.1
  • Spring 4.x
  • Java 1.8

测试程序目录结构

这里写图片描述

源代码&开发步骤

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.smart</groupId>
    <artifactId>chapter13</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${commons-dbcp.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.2</version>
                <configuration>
                    <forkMode>once</forkMode>
                    <threadCount>10</threadCount>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <file.encoding>UTF-8</file.encoding>
        <spring.version>4.2.2.RELEASE</spring.version>
        <slf4j.version>1.7.5</slf4j.version>
        <junit.version>4.10</junit.version>
        <mysql.version>5.1.29</mysql.version>
        <commons-dbcp.version>1.4</commons-dbcp.version>
        <jetty.version>8.1.8.v20121106</jetty.version>
    </properties>

</project>

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sampledb
jdbc.username=username
jdbc.password=password

log4j.properties

log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描包以注册注解声明的bean-->
    <context:component-scan base-package="com.smart" />

    <!--配置数据源-->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" />

    <!--声明JdbcTemplate Bean-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
          p:dataSource-ref="dataSource" />

    <!--声明事务,如果没有声明事务,则在测试过程中将报"找不到事务bean"的错误 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource"/>

</beans>

ForumDao.java

package com.smart.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ForumDao {

    private JdbcTemplate jdbcTemplate;

    public void initDb(){
        String sql = "create table t_user(user_id int primary key, user_name varchar(60))";
        jdbcTemplate.execute(sql);
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}

ForumDaoTest.java

package com.smart.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.Test;

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class ForumDaoTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private ForumDao forumDao;

    @Test(enabled = true)
    public void testInitDb(){
        forumDao.initDb();
    }

}
查看评论

C/C++单元测试培训

-
  • 1970年01月01日 08:00

Spring4+Hibernate4+junit4环境搭建

Spring4+Hibernate4+junit4环境搭建
  • lipei1220
  • lipei1220
  • 2015-06-09 20:32:51
  • 2635

spring MVC mock类单元测试(controller)

Spring从J2EE的Web端为每个关键接口提供了一个mock实现: MockHttpServletRequest 几乎每个单元测试中都要使用这个类,它是J2EE Web应用程序最常用...
  • dubinglin
  • dubinglin
  • 2017-04-06 15:23:56
  • 954

记录一次spring+dubbo下的框架单元测试和所遇到的坑

记录一次spring+dubbo下的框架单元测试 环境 spring:4.0 dubbo:2.8.4 Eclipse:Neon.3 Release (4.6.3) maven:4.0.0 ...
  • xuefeng1207
  • xuefeng1207
  • 2017-04-20 15:11:00
  • 1472

spring boot学习系列:spring boot的单元测试实战

介绍一下springboot的单元测试mock模拟工具
  • u013871100
  • u013871100
  • 2017-08-17 23:27:08
  • 969

maven下使用junit对spring进行单元测试_01基本应用

一、开发环境二、文件清单pom.xmlApplicationContext.xmlEmployee.java三、主要步骤1.使用springframework提供的单元测试2.基本使用3.封装基于Ab...
  • wanglha
  • wanglha
  • 2015-02-12 15:32:44
  • 1661

spring工程的单元测试用例加载配置方法

spring开发时可以使用基于java注解的配置,也可以使用基于xml配置文件的配置方法。大多数情况下在开发过程中需要编写单元测试用例针对不同的模块进行独立测试用于验证独立的功能,本文介绍在基于不同的...
  • yxtouch
  • yxtouch
  • 2017-04-25 16:57:47
  • 1991

IntelliJ IDEA单元测试+SpringMvc单元测试

Intellij idea + Junit 单元测试 原文链接:http://blog.csdn.net/u011138533/article/details/52165577 本文按以下...
  • xn_28
  • xn_28
  • 2017-03-06 15:56:43
  • 1359

使用Spring-Test对Spring框架进行单元测试

使用Spring-Test对Spring框架进行单元测试配置过程:加载依赖:引入Maven依赖: org.springframework ...
  • u011389474
  • u011389474
  • 2017-05-16 16:24:34
  • 5278

spring boot 单元测试

引入maven依赖 org.springframework.boot spring-boot-starter-test tes...
  • u014695188
  • u014695188
  • 2016-08-20 18:53:11
  • 21721
    个人资料
    持之以恒
    等级:
    访问量: 6125
    积分: 433
    排名: 11万+
    最新评论