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

}
发布了24 篇原创文章 · 获赞 2 · 访问量 438

猜你喜欢

转载自blog.csdn.net/qq_42107430/article/details/103321894
今日推荐