MENU

整理一下Spring整合Junit4单元测试方法

• February 22, 2020 • Read: 49 • 技术笔记,SSM框架

很多时候我们需要用到Junit4进行单元测试,这里我整个一下大致流程,方便以后查看。

添加依赖

<dependency> 
  <groupId>junit</groupId>  
  <artifactId>junit</artifactId>  
  <version>4.13</version>  
  <scope>test</scope> 
</dependency>
<dependency> 
  <groupId>org.springframework</groupId>  
  <artifactId>spring-test</artifactId>  
  <version>5.2.3.RELEASE</version>  
  <scope>test</scope> 
</dependency>

注意:必须在test目录中使用才有这两个注解。
@Runwith(SpringJUnit4ClassRunner.class) 加载Junit4
@ContextConfiguration("classpath:spring.xml") 引入配置文件

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-dao.xml")
public class test1 {

    @Autowired
    DepartmentDao departmentDao;
    
    @Test
    public void test(){
        Department department = new Department();
        department.setSn("100");
        department.setAddress("gd");
        department.setName("gg");
        departmentDao.insert(department);
    }

    @Test
    public void test2(){
        Department select = departmentDao.select("10003");
        System.out.println(select.getAddress());
    }
}
本站所有未注明转载的文章均为原创,并采用CC BY-NV-SA 4.0 授权协议,转载请注明来源。

Last Modified: April 7, 2020
Archives QR Code
QR Code for this page
Tipping QR Code