How to inject real mapper objects when spring framework gives unit testing to service layer

This method is provided in the crud interface of

mybatis-plus

/**
 * 

* entity *

* * @param queryWrapper null * @return */ List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

by using the wrapper object, it is more necessary to provide different query conditions when called in the service layer, such as

List<User> plainUsers3 = userMapper.selectList(new LambdaQueryWrapper<User>()
                .nested(i -> i.eq(User::getRoleId, 2L).or().eq(User::getRoleId, 3L))
                .and(i -> i.ge(User::getAge, 20)));

then there is a problem. If there is a problem with the logic of the incoming QueryWrapper object itself, there is no way to test the problem using the mock mapper object.

when writing tests to the dao layer, the following comments will be added to the test classes

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Import Properties -->
    <context:property-placeholder location="classpath:config.properties"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
          destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="filters" value="stat"/>
        <property name="initialSize" value="1"/>
        <property name="minIdle" value="1"/>
        <property name="maxActive" value="20"/>
        <property name="maxWait" value="60000"/>
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="minEvictableIdleTimeMillis" value="300000"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="maxOpenPreparedStatements" value="20"/>
        <property name="asyncInit" value="true"/>
    </bean>

    <bean id="sqlSessionFactory"
          class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="globalConfig" ref="globalConfig"/>
        <property name="mapperLocations" value="classpath*:mybatis/mapper/*.xml"/>
        <property name="configLocation" value="classpath:mybatis-config.xml">

        </property>

        <property name="plugins">
            <array>
                <bean class="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor"/>
            </array>
        </property>
    </bean>

    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <property name="dbConfig" ref="dbConfig"/>
    </bean>

    <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
        <property name="idType" value="AUTO"/>
    </bean>

    <!-- MyBatis Mapper Scan Config  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage"
                  value="com.example.demo.mapper"/>
    </bean>

</beans>

and use the configuration file to inject the dependency and run the test.

so how can I modify this configuration file to inject real mapper objects into service when running unit tests in the service layer?

Jul.04,2022

Unit tests do not rely on real databases. It is recommended to simulate data through mock.
easymock,mockito can be

Menu