1、引入相关的依赖
<!--单元测试-->
//将Junit集成到spring中去,其中已经包括了spring-context等多个包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
2、搭建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解扫描,希望处理service和dao,controller不需要spring框架来处理
所以配置扫描controller-->
<context:component-scan base-package="com.it">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- spring 的配置文件,这里主要配置和业务逻辑有关的 -->
<!-- 配置数据源信息 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>-->
<!--2、配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--===========Spring整和Mybatis的配置===========-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis全局配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!--指定数据源-->
<property name="dataSource" ref="dataSource" />
<!--指定mapper映射文件位置-->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!--配置扫描器,spring启动时将mybatis的接口映射为类
/也就是将mybatis接口的实现加入到IOC容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描mapper接口的实现,加入到IOC容器中-->
<property name="basePackage" value="com.it.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
3、编写测试类代码
@RunWith:用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入,spring提供org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为Junit测试环境
@ContextConfiguration({"classpath:applicationContext.xml","classpath:spring/buyer/applicationContext-service.xml"})
导入配置文件,这里我的applicationContext配置文件是根据模块来分类的。如果有多个模块就引入多个“applicationContext-service.xml”文件。如果所有的都是写在“applicationContext.xml”中则这样导入: @ContextConfiguration(locations = "classpath:applicationContext.xml")
由于我这里使用的是配置文件配置环境如果使用配置类加载的话可以选择加载配置类,可以改为@ContextConfiguration(classes = {TestConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class ssm {
@Autowired
private EmployeeService employeeService;
@Test
public void test1(){
Employee employee = employeeService.login("017","123");
System.out.println(employee.getSn()+"\t"+employee.getName());
}
}
踩坑:两个String类型的字符串进行比较时不能用”==“,否则即使相等也是false。其次,测试方法最好不要打印对象的属性,否侧当没有查询到对象,返回null的时候,打印对象属性的时候会抛出空指针异常,误导你,以为没有查询成功,实则已经成功了。