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));
}
}