您的位置:首页 > 编程语言 > Java开发

给项目添加spring的测试单元

2017-12-21 00:00 387 查看
1. 首先依赖两个包(junit-*.jar和spring-test-*RELEASE.jar。)尽量拿最新的即可

<!--单元测试-->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>

2. 然后建立一个测试类

package com.skyautobuild.test;

import com.skyautobuild.service.ProjectService;
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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/confg/applicationContext.xml"})
public class ProjectTest {

@Autowired
private ProjectService projectService;
@Test
public void test(){

projectService.addProject();
}

}

3. @RunWith(SpringJUnit4ClassRunner.class) 注明测试单元

4. @ContextConfiguration(locations = {"classpath:/confg/applicationContext.xml"})

因为不是通过web.xml的启动方式去启动配置文件加载spring容器,所以这里@ContextConfiguration引入spring容器的配置文件,以启动所需的spring容器。如下就是需要启动的一个service bean 容器

5. @Autowired
private ProjectService projectService;

有了第4步的启动spring容器之后,这里方能注入。

6. applicationContext.xml容器配置文件

1. 最基本的是扫描要通过注解(@Autowired)注入的包

<context:component-scan base-package="com.skyautobuild.service"/>

2. 通过spring jdbcTemplate模板对数据库进行操作

1)配置数据源

<context:property-placeholder location="classpath:/confg/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}"/>

2)根据数据源准备好jdbcTemplate Bean

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

3) 配置需要注入jdbcTemplate的包(报下所有)

<context:component-scan base-package="com.skyautobuild.dao"/>

4) 两层注解注入POJO中(必须一层service加一层DAO,缺一不可,不能直接跳过service去直接调用DAO)

@service (service)

package com.skyautobuild.service;
import com.skyautobuild.dao.ProjectDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProjectService {

@Autowired
private  ProjectDao projectDao;
public void addProject(){
projectDao.addProj();
}
}

@Repository (DAO)

package com.skyautobuild.dao;

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

@Repository
public class ProjectDao {

@Autowired
private JdbcTemplate jdbcTemplate;
public void addProj(){
jdbcTemplate.update("insert into project(numb,text) values('weidebo','WDB')");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息