很多时候我们需要用到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());
    }
}

Last modification:September 28, 2020
如果觉得我的文章对你有用,请随意赞赏