给项目添加spring的测试单元

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')");

    }
}

 

转载于:https://my.oschina.net/u/3697586/blog/1593422

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值