springboot进行Junit测试

话不多说,直入正题。
添加依赖

<!--SpringJUnit4ClassRunner测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.2.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>

测试类中进行测试

@SpringBootTest(classes = {CmfzdemoApplication.class})	//必写 大括号中的值为启动类的名称
@RunWith(SpringJUnit4ClassRunner.class)	//必写
public class AppTest {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private AlbumService albumService;

    @Test
    public void Test1(){
        System.out.println(articleService.getArticle());
    }

    @Test
    public void Test2(){
        System.out.println(albumService.getAlbum());
    }

}

1.添加所需依赖
2.测试类上添加 SpringBootTest RunWith 注解
3.SpringBootTest 注解中大括号的值填写启动类的名称,RunWith 注解照抄
4.方法名上添加Test注解

上述四部完成后,即可进行方便快捷的springjunit测试。
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!
欢迎加入技术群聊!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/taiguolaotu/article/details/107042832