之前開發測試程序功能時,采取的都是通過連接訪問來測試程序,常用的方法有:
1、直接通過在瀏覽器輸入網址訪問;
2、通過公司搭建的eolinker來進行訪問測試;
3、通過編寫python腳本來進行測試;
4、通過postman工具來測試。
但這樣經常會一測就要測一整塊,相對單元測試來說定位問題比較麻煩,單元測試能幫助我們拆分方法,單獨測試些關鍵的代碼、功能,是日常開發中必備的技能,同時也是各大公司招技術人員的必要要求之一。但今天看了不少文章,也試了不少,發現即是很簡單的東西自己也走了不少彎路,所以覺得很有必要把今天的練習寫下來。
1、首先是pom.xml的包
<!-- test依賴,必須添加 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2、下面分別是controller層,service層,jpaRepository層
DemoController.class類
package com.test.demo.controllers;
import com.test.demo.domain.entities.Address;
import com.test.demo.domain.entities.AddressRepository;
import com.test.demo.services.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/hello")
public class DemoController {
@Autowired
private DemoService demoService;
@Autowired
private AddressRepository addressRepository;
/**
* RequestParam 參數里面的name和value的效果是一樣的
* RequestMapping 參數里面就只能是value了,
* @param name
* @return
*/
@RequestMapping(value = "/queryaddress")
public String demo(@RequestParam(name = "name")String name){
List<Address> addressList = demoService.queryAddress("%"+name+"%");
System.out.println(addressList.toString());
return addressList.toString();
}
}
DemoService.class類
package com.test.demo.services;
import com.test.demo.domain.entities.Address;
import com.test.demo.domain.entities.AddressRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DemoService {
@Autowired
private AddressRepository addressRepository;
/**
* 根據名稱查找地區
* @param name
* @return
*/
public List<Address> queryAddress(String name){
return addressRepository.queryListByName(name);
}
}
AddressRepository.class類
package com.test.demo.domain.entities;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface AddressRepository extends JpaRepository<Address, Integer>{
/**
* 根據地區查找數據
* @param name
* @return
*/
@Query(value = "select * from address where address like ?",nativeQuery = true)
List<Address> queryListByName(String name);
}
3、下面是新建測試類
package com.test.demo.controllers;
import com.test.demo.Main;
import com.test.demo.domain.entities.Address;
import com.test.demo.domain.entities.AddressRepository;
import com.test.demo.services.DemoService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Main.class)//掃描啟動類
public class DemoTest {
@Autowired
private AddressRepository addressRepository;
@Autowired
private DemoController demoController;
@Autowired
private DemoService demoService;
/**
* 查找地址
* 訪問addressRepository層
*/
@Test
public void query() {
System.out.println("===訪問addressRepository層====");
List<Address> lst = addressRepository.queryListByName("%我%");
System.out.println(lst.size());
if (lst.size() > 0) {
for (Address ad : lst) {
System.out.println(ad.toString());
}
}
}
/**
* 查找地址,訪問controller層
*/
@Test
public void queryAddress(){
System.out.println("===訪問controller層====");
String str = demoController.demo("%我%");
System.out.println(str);
}
/**
* 查找地址,訪問service層
*/
@Test
public void queryServiceAddress(){
System.out.println("===訪問service層====");
List<Address> lst = demoService.queryAddress("%我%");
System.out.println(lst.size());
if (lst.size() > 0) {
for (Address ad : lst) {
System.out.println(ad.toString());
}
}
}
}
測試:
點擊紅箭頭所指位置,可以進行想應的測試:
測試結果:
應用實例已放到github上:https://github.com/DYH2020/springBootDemo
參考項目:https://github.com/BraveWangDev/SpringBoot/tree/master/SpringBoot-Junit