请注意,本文编写于 1519 天前,最后修改于 1301 天前,其中某些信息可能已经过时。
很多时候我们需要用到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());
}
}
版权属于:乐心湖's Blog
本文链接:https://www.xn2001.com/archives/375.html
声明:博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!