spring junit4 测试web项目

ps:一般来说业务主要是service层,所以本文是对service层的测试,要是想测试controller层,访问url,可以使用mock

1.依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

2.创建测试目录,测试包,测试类

工程目录如下

解释:这里将项目上使用的配置文件放到测试目录的resource下,当运行测试类时,会自动加载配置文件

这里我的配置文件中主要是数据库连接配置,因为spring的xml配置中有数据库连接池,要连接数据库所以需配置文件

3.代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/spring/自己的配置文件.xml"})
//@Transactional(transactionManager = "dataSourceTransactionManager")
public class Tests1 extends AbstractTransactionalJUnit4SpringContextTests{
    @Autowired
    private NoteManager noteManager;

    @Test
    @Rollback
    public void insert() throws Exception {
        NoteDO noteDO = new NoteDO();
        noteDO.setId(8);
        noteDO.setItemId("qaz");
        noteDO.setNote("测试");
        noteDO.setShare(true);
        noteDO.setType(0);
        noteDO.setUserId("admin");
        int insert = noteManager.insert(noteDO);
        System.out.println(insert);
    }

}

ps:

@RunWith:这个是指定使用的单元测试执行类,这里就指定的是SpringJUnit4ClassRunner.class;

@ContextConfiguration:这个指定spring配置文件所在的路径,可以同时指定多个文件;

@Test标注在方法前,表示其是一个测试的方法 无需在其配置文件中额外设置属性.

@Transactional(transactionManager = "dataSourceTransactionManager") 事务控制,执行完任务会回滚,不会造成脏数据。

或者是继承类 AbstractTransactionalJUnit4SpringContextTests

@Rollback 是否回滚,方法级注解,默认是true。

5.spring配置文件

<?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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.x.s.n"/>
    <context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true" location="classpath:ooxx.properties" order="1"/>
    <context:property-placeholder ignore-unresolvable="true" ignore-resource-not-found="true" location="classpath:xxoo.properties" order="2"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${metadata.url}"/>
        <property name="username" value="${metadata.username}"/>
        <property name="password" value="${metadata.password}"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="50"/>
        <property name="maxActive" value="20"/>
        <property name="initialSize" value="1"/>
        <property name="maxWait" value="60000"/>
        <property name="minIdle" value="1"/>
    </bean>

    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:oo.sql"/>
    </jdbc:initialize-database>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mappers/Note.xml"/>
        <!--当测试使用事务的时候,需要将mybatis的事务注释掉-->
       <!-- <property name="transactionFactory">
            <bean class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory"/>
        </property>-->
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg ref="sqlSessionFactory"/>
    </bean>
    <mybatis:scan base-package="com.x.s.n.dao"/>

    <!--以下是为了支持测试事务单独配置的,自行决定是否需要-->
    <!--配置事务注解扫描-->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
    <!--声明事务-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值