使用@SpringBootTest注解进行单元测试

1、pom.xml文件中引入test包依赖,如下:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2、编写测试类 DemoApplicationTests.java,加入@SpringBootTest注解

@RunWith(SpringRunner.class)
@SpringBootTest(classes={DemoApplication.class})   //指定启动类
class DemoApplicationTests {
    @Autowired
    private ListService service;

    @Test
    void contextLoads() {
        //TODO 此处调用接口方法
        service.listTest();
        System.out.println("contextLoads");
    }

    @Test
    public void testTwo(){
        service.listTest();
        System.out.println("test hello 2");
    }

    @Before
    public void testBefore(){
        System.out.println("before");
    }

    @After
    public void testAfter(){
        System.out.println("after");
    }


}

结果

 

发布了97 篇原创文章 · 获赞 31 · 访问量 19万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章