1、在pom增加junit和spring-test
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> </dependency>
2、创建BaseJunit4Test 类
import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //加载配置文件 public class BaseJunit4Test { }
3、创建单元测试
public class XXXMapperTest extends BaseJunit4Test { @Autowired private XXXMapper xxxMapper; @Test public void queryList() throws Exception { List<UserInfo> list = xxxMapper.queryList(); Assert.assertTrue(list.size() > 0); } }
以上是对Dao层访问数据的测试。