日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

springboot 单元测试

發(fā)布時(shí)間:2025/3/21 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 单元测试 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  之前開發(fā)測(cè)試程序功能時(shí),采取的都是通過連接訪問來測(cè)試程序,常用的方法有:

1、直接通過在瀏覽器輸入網(wǎng)址訪問;

2、通過公司搭建的eolinker來進(jìn)行訪問測(cè)試;

3、通過編寫python腳本來進(jìn)行測(cè)試;

4、通過postman工具來測(cè)試。

  但這樣經(jīng)常會(huì)一測(cè)就要測(cè)一整塊,相對(duì)單元測(cè)試來說定位問題比較麻煩,單元測(cè)試能幫助我們拆分方法,單獨(dú)測(cè)試些關(guān)鍵的代碼、功能,是日常開發(fā)中必備的技能,同時(shí)也是各大公司招技術(shù)人員的必要要求之一。但今天看了不少文章,也試了不少,發(fā)現(xiàn)即是很簡(jiǎn)單的東西自己也走了不少?gòu)澛?#xff0c;所以覺得很有必要把今天的練習(xí)寫下來。

?

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 參數(shù)里面的name和value的效果是一樣的
* RequestMapping 參數(shù)里面就只能是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;

/**
* 根據(jù)名稱查找地區(qū)
* @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>{

/**
* 根據(jù)地區(qū)查找數(shù)據(jù)
* @param name
* @return
*/
@Query(value = "select * from address where address like ?",nativeQuery = true)
List<Address> queryListByName(String name);
}

?

3、下面是新建測(cè)試類

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)//掃描啟動(dòng)類
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());
}
}
}

}

?

測(cè)試:

點(diǎn)擊紅箭頭所指位置,可以進(jìn)行想應(yīng)的測(cè)試:

?

測(cè)試結(jié)果:

?

?

?

?

?應(yīng)用實(shí)例已放到github上:https://github.com/DYH2020/springBootDemo

?

?參考項(xiàng)目:https://github.com/BraveWangDev/SpringBoot/tree/master/SpringBoot-Junit

?

轉(zhuǎn)載于:https://www.cnblogs.com/dyh2025/p/9251144.html

總結(jié)

以上是生活随笔為你收集整理的springboot 单元测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。