Spring Boot學習筆記(六)整合Junit
阿新 • • 發佈:2018-12-22
第一步,在pom.xml檔案中增加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
第二步,準備好啟動類
package org.test; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan //掃描servlet註解 public class Application { public static void main(String[] args) { org.springframework.boot.SpringApplication.run(Application.class, args); } }
第三步,新建測試類,並設定註解
@RunWith: 整合Junit與spring
@SpringBootTest:載入Spring Boot啟動類
在方法上用加上@test註解就可以執行測試方法了
package org.test.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.test.Application; import org.test.entity.TestTable; import org.test.service.TestTableService; import javax.annotation.Resource; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) //整合junit和spring環境整合 @SpringBootTest(classes = {Application.class}) //載入Spring Boot啟動類 public class GeZhongTest { @Resource private TestTableService service; @Test public void test(){ List<TestTable> list = service.findAll(); System.out.println(list.spliterator().toString()); } }
另:
要在控制檯顯示mybatis執行的sql語句的配置(application.yml)如下
logging:
level:
org.test.dao : debug