<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.3.14version>
dependency>
注意:junit的依赖至少要是4.12版本,否则出现如下异常:
java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
// 使用Spring整合Junit专用的类加载器
@RunWith(SpringJUnit4ClassRunner.class)
// 加载配置文件或者配置类
//@ContextConfiguration(classes = {SpringConfiguration.class}) //加载配置类
@ContextConfiguration(locations = {"classpath:spring.xml"})//加载配置文件
public class UserTest {
@Autowired
private UserService userService;
@Test
public void selectAllUser() {
List<User> users = userService.selectAllUser();
for (User i : users) {
System.out.println(i);
}
}
}