Junit4单元测试
- 在maven中加入Junit4依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- 指定范围,在测试时才会加载 -->
<scope>test</scope>
</dependency>
- 在src/test/java文件夹下面新建XXXTest测试类
//配置spring和junit整合,这样junit在启动时就会加载spring容器
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring的配置文件
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class CommentTest {
//注入Service层实现类
@Autowired
private CommentService commentService;
//注意!@Test不可少
@Test
public void studentTest() {
System.out.println(commentService.listComment(2));
}
}