Spring 集成Junit单元测试

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层访问数据的测试。 

 

原文地址:https://www.cnblogs.com/linlf03/p/9982467.html