1.加入maven依赖
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.3.RELEASE</version>
<scope>test</scope>
</dependency>
2.创建基础测试类,其余的测试类都会继承这个基础测试类
package com.ishop.base;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by GanBaby on 2018/10/26
* 配置spring和junit整合,junit启动时加载springIOC容器
*/
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件
@ContextConfiguration("classpath:spring/applicationContext-*.xml")
public class BaseTest {
}
这是我的配置文件所在的目录,因为有多个,所以用*全部扫描
3.创建测试类,继承刚刚创建的BaseTest
package com.ishop.test;
import com.ishop.base.BaseTest;
import com.ishop.service.user.TcUserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;
/**
* Created by GanBaby on 2018/10/26
*/
public class Test extends BaseTest {
@Autowired
private TcUserService tcUserService;
@Test
public void test(){
List<Map<String, Object>> list = tcUserService.selectList();
System.out.printf("我执行了"+list.get(1).get("userSex"));
}
}
4.走一波
我用的是idea运行方式如下,直接点击类或者方法左边的绿色箭头都行,下面是图解
看看结果
如果感觉这样不好找结果,大家可以debug运行