- 導(dǎo)入maven依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
- 編寫測(cè)試類
// 表示繼承了SpringJUnit4ClassRunner類
@RunWith(SpringJUnit4ClassRunner.class)
//加載spring容器
@ContextConfiguration(locations = { "classpath:applicationContext.xml"})
public class test {
@Autowired
private StudentMapper studentMapper;
@Test
public void test01(){
List<Student> students = studentMapper.selectAllStudent();
System.out.println(students);
}
}
注意:
- spring-test的大版本號(hào)要和你的spring版本對(duì)應(yīng)。
- ssm和springboot還有區(qū)別的。springboot 單元測(cè)試寫完@test之后就可以測(cè)試。
但是ssm需要加上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml"})這兩個(gè)注解。