添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
新建測試類
在web項目(即含有SpringApplication啟動類)中test目錄新建測試類, 包路徑和SpringApplication中的掃描路徑一致,否則無法啟動測試類。若測試類的包路徑和啟動類的包路徑不一致,會出現以下錯誤信息:
Neither @ContextConfiguration nor @ContextHierarchy found for test class [xx.xx.Test], using SpringBootContextLoader
Could not detect default resource locations for test class [xx.xx.Test]: no resource found for suffixes {-context.xml, Context.groovy}.
Could not detect default configuration classes for test class [xx.xx.Test]: Test does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
測試類添加
package xx.xx.test;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests {
@Before
public void init(){
System.out.println("******測試開始");
}
@After
public void end(){
System.out.println("******測試結束");
}
@BeforeClass
public static void initClass(){
System.out.println("******測試開始初始化");
}
@AfterClass
public static void endClass(){
System.out.println("******測試結束初始化");
}
}
package xx.xx.test;
public class Test extends ApplicationTests{
@Autowired
private BtsTradeGoodsService btsTradeGoodsService;
@org.junit.Test
public void add(){
btsTradeGoodsService.add();
}
}
然后在有@Test注解方法的類中使用JUint啟動。 在@Test注解的方法中,和平時開發項目調用接口是一樣的。
注解
類注解
@RunWith:
1.表示運行方式,@RunWith(JUnit4TestRunner)、@RunWith(SpringRunner.class)、@RunWith(PowerMockRunner.class) 三種運行方式,分別在不同的場景中使用。
2.當一個類用@RunWith注釋或繼承一個用@RunWith注釋的類時,JUnit將調用它所引用的類來運行該類中的測試而不是開發者去在junit內部去構建它。我們在開發過程中使用這個特性。
@SpringBootTest:
1.注解制定了一個測試類運行了Spring Boot環境。提供了以下一些特性:
1.1.當沒有特定的ContextConfiguration#loader()(@ContextConfiguration(loader=...))被定義那么就是SpringBootContextLoader作為默認的ContextLoader。
1.2.自動搜索到SpringBootConfiguration注解的文件。
1.3.允許自動注入Environment類讀取配置文件。
1.4.提供一個webEnvironment環境,可以完整的允許一個web環境使用隨機的端口或者自定義的端口。
1.5.注冊了TestRestTemplate類可以去做接口調用。
2.添加這個就能取到spring中的容器的實例,如果配置了@Autowired那么就自動將對象注入。
@WebAppConfiguration:
由於是Web項目,Junit需要模擬ServletContext,因此我們需要給我們的測試類加上@WebAppConfiguration。
@WebIntegrationTest("server.port:0"):
使用0表示端口號隨機,也可以具體指定如8888這樣的固定端口。不可和@WebAppConfiguration同時使用。
方法注解
@BeforeClass:方法只能是static void。
@AfterClass:方法只能是static void。
@Before:@Test運行之前調用的方法,可以做初始化操作
@After:執行完測試用例需要執行的清理工作
@Test:測試用例的單元
@Mock:
有點類似Autowired注解,而@Mock注解是自動實現模擬對象,而並非Bean實例創建。
正式環境只是一個接口,並沒有實現,也並沒有納入spring容器進行管理。使用BDDMockito對行為進行預測。
@Ignore("not ready yet"):該方法不執行
執行順序是:@BeforeClass→@Before→@Test→@After→@AfterClass
當啟動測試類,測試類中有多個@Test,@BeforeClass和@AfterClass只會執行一次,每一個@Test都會執行一次@Before和@After。
對Controller進行測試
1、使用瀏覽器直接訪問:
http://localhost:8080/index
http://localhost:8080/show?id=100
2、使用測試類:
package xx.xx.ctrl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserCtrl {
@GetMapping("/index")
public String index(){
System.out.println("UserCtrl.index");
return "UserCtrl.index";
}
@GetMapping("/show")
public String show(@RequestParam("id")String id){
System.out.println("UserCtrl.show:" + id);
return "UserCtrl.show:" + id;
}
}
package xx.xx.test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTests {
@Before
public void init(){
System.out.println("******測試開始");
}
@After
public void end(){
System.out.println("******測試結束");
}
@BeforeClass
public static void initClass(){
System.out.println("******測試開始初始化");
}
@AfterClass
public static void endClass(){
System.out.println("******測試結束初始化");
}
}
package xx.xx.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
public class Test extends ApplicationTests{
@Autowired
private TestRestTemplate testRestTemplate;
@org.junit.Test
public void toIndex(){
String t = testRestTemplate.getForObject("/index", String.class);
System.out.println("toIndex:" + t);
}
@org.junit.Test
public void toShow(){
String t = testRestTemplate.getForObject("/show?id=99", String.class);
System.out.println("toShow:" + t);
}
@Override
public void init(){
System.out.println("******重寫測試開始");
}
}
使用這種方式,在@SpringBootTest注解中一定要添加`webEnvironment = SpringBoot