一、pom.xml
<!--定义版本号 ${org.springframework.version}-->
<org.springframework.version>5.0.7.RELEASE</org.springframework.version>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
二、test包下测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MyTest {
@Autowired
private UserService userService;
@Test
public void sendAdvanceNotice() {
userService.getUser("test");
}
}
三、执行方法
要执行测试方法,只需要在方法名或者方法内右键,就会发现有run选中的方法名(Intelj Idea中)。
本文介绍了如何在Spring Boot 5.0项目中使用Junit 4.12进行单元测试,并配置了Spring Test依赖。重点展示了如何在test包下的测试类中注入UserService并执行sendAdvanceNotice方法的测试。
1233

被折叠的 条评论
为什么被折叠?



