Spring集成Junit4单元测试
2016-12-28 18:13
363 查看
1、maven依赖
<properties> <org.springframework.version>4.2.4.RELEASE</org.springframework.version> <java.version>1.7</java.version> <maven.compiler.version>3.1</maven.compiler.version> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Spring Test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2、注解配置
package com.ricky.codelab.spring; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.ricky.codelab.spring.aop.service.HelloService; /** * Spring4 + Junit4集成 * @author Ricky * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring-annotation.xml") public class HelloServiceTest { @Autowired private HelloService helloService; @Test public void testHello(){ helloService.sayHello("ricky"); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
解释下用到的注解:
@RunWith:用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入,spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为Junit测试环境
@ContextConfiguration:用于指定spring配置环境
@TransactionConfiguration:用于配置事务
@Transactional:表示所有类下所有方法都使用事务
spring-annotation.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.ricky.codelab.spring" /> </beans>
相关文章推荐
- Spring集成Junit4单元测试
- Spring集成Junit4单元测试
- Spring集成Junit4单元测试
- Spring集成Junit4单元测试
- Spring集成Junit4单元测试
- Spring集成Junit4单元测试
- Spring整合Junit4进行单元测试
- Spring集成JUnit单元测试框架
- Spring整合Junit4进行单元测试
- Java Web(5) Spring 下使用Junit4 单元测试
- JUnit4结合spring做单元测试
- Unitils集成DBUnit、Spring-单元测试
- 使用Spring注解方式注入properties文件内容,并配合Junit4+Spring做单元测试
- 在Spring中结合Dbunit对Dao进行集成单元测试
- Spring2.5+JUnit4单元测试
- Maven管理的Spring Web项目集成JUnit单元测试
- Maven管理的Spring Web项目集成JUnit单元测试
- 使用spring提供的测试功能结合JUnit4进行单元测试
- spring 和hibernate 集成 时单元测试