IDEA中SSM項目單元測試

 Junit4單元測試

 

  1. 在maven中加入Junit4依賴
     <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <!-- 指定範圍,在測試時纔會加載 -->
                <scope>test</scope>
            </dependency>

     

  2. 在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));
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章