SpringTest(一)

SpringMvcTest总结:

最近要做单元测试,所以选择的是SpringTest这个测试框架。

1.准备工作。(导入jar包)

因为使用Maven管理jar包,所以在要做单元测试的模块中的pom文件中加入如下代码:

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</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-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-oxm</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.8.5</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

当然你可以将上述代码写在父模块中,那么就不必每个子模块中都重复写入上面的代码。

2.编写测试类。

@RunWith

这是一个类注解,表明该测试类的执行类是什么。如果没有,测试执行类会是默认的。

常见的测试执行类:

(1)SpringJUnit4ClassRunner.class:

(2)Parameterized.class:参数化运行器,配合@Parameters使用junit的参数化功能

(3)JUnit4.class:junit4的默认运行器

(4)Suite.class,用法如下:

@RunWith(Suite.class)

@SuiteClasses({ATest.class,BTest.class,CTest.class})

当执行有上面标注的测试类,那么ATest.class,BTest.class,CTest.class等多个测试类也会执行。

@ContextConfiguration 注解,这是一个类级别的注解。

该注解有以下两个常用的属性:

locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。如下所示:

@ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”})。如果没有设置该属性,

那么,会在默认位置找配置文件,默认配置文件为“测试类名-context.xml”

inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。

3.运行测试。

因为本人搭建的是Maven项目,所以我会把每个模块的测试类放在对应模块的src/test目录下,那么当我执行"mvn clean test"的时候,

maven会自动执行src/test目录下的所有测试类中的测试用例。十分方便,不用逐个点击测试用例进行点击。

存在问题:

还没有对rest服务的测试进行研究,还有dao层,service层。所以还需要继续学习。

时间: 2024-08-30 04:23:24

SpringTest(一)的相关文章

SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

JUnit单元测试用例中使用Spring框架,之前我的使用方式很直接. /** * 用于需要用到Spring的测试用例基类 * * @author lihzh * @alia OneCoder * @blog http://www.coderli.com */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/spring/applicationContext.xml" }) pu

spring测试父类,使用junit-4.4.jar,spring-test.jar

@ContextConfiguration(locations = "classpath:conf/applicationContext.xml") @RunWith(SpringJUnit4ClassRunner.class) @Transactional @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) public abstr

junit4 (三)和spring-test结合使用

使用注解简化了可以和Junit文章相比较看出. import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springfra

TestNG 集成Spring-test

需要集成上一篇博客 <TestNG单元测试> 导包              <dependency>                    <groupId>org.springframework</groupId>                    <artifactId>spring-test</artifactId>                    <version>4.1.9.RELEASE</v

org.springframework spring-test

需要的jar包 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.3.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId&

Spring-test使用JUnit时,测试类autowired报错,create bean error

Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在网上提问,确认我一定是在扫描包上出现了问题.但是controller里面明明是可以得啊. 等等,我是使用maven构建的项目,项目分成了main.test两个部分.配置文件都在main文件夹下,那么扫到的包相应的都是mian文件夹下的包,而测试类所在的包扫描不到也就理所当然了. 解决方案:暂时先给t

spring-test使用介绍

一.首先引入spring的jar文件到项目中,我采用maven管理项目依赖的jar包: <properties> <spring.version>4.0.0.RELEASE</spring.version> </properties> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactI

架构实例之SpringTest

架构实例之SpringTest 1.开发工具和开发环境       开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13 开发环境:WIN7 2.SpringTest实现功能 用户登录.用户注册.退出登录. 3.SpringTest使用技术       本实例使用了JSP.Spring3.1框架.JdbcTemplate来实现用户登录.用户注册和退出登录功能.系统架构图如图一所示:   图一:SpringTest系统架构图 4

【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/spring/applicationContext.xml" }) public class SpringTest {} 想要获取Applicat