摘要:本文将带你了解测试技术springboot 单元测试,希望对大家学测试技术有所帮助。
本文将带你了解测试技术springboot 单元测试,希望对大家学测试技术有所帮助。
之前开发测试程序功能时,采取的都是通过连接访问来测试程序,常用的方法有:
1、直接通过在浏览器输入网址访问;
2、通过公司搭建的eolinker来进行访问测试;
3、通过编写python脚本来进行测试;
4、通过postman工具来测试。
但这样经常会一测就要测一整块,相对单元测试来说定位问题比较麻烦,单元测试能帮助我们拆分方法,单独测试些关键的代码、功能,是日常开发中必备的技能,同时也是各大公司招技术人员的必要要求之一。但今天看了不少文章,也试了不少,发现即是很简单的东西自己也走了不少弯路,所以觉得很有必要把今天的练习写下来。
1、首先是pom.xml的包
org.springframework.boot
spring-boot-starter-test
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){
ListaddressList = 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 ListqueryAddress(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{
/**
* 根据地区查找数据
* @param name
* @return
*/
@Query(value = "select * from address where address like ?",nativeQuery = true)
List 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 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 lst = demoService.queryAddress("%我%");
System.out.println(lst.size());
if (lst.size() > 0) {
for (Address ad : lst) {
System.out.println(ad.toString());
}
}
}
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标软件测试之测试技术频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号