使用@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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章