之前开发测试程序功能时,采取的都是通过连接访问来测试程序,常用的方法有:
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层
扫描二维码关注公众号,回复:
1823713 查看本文章
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 = "/demo") public String demo(@RequestParam(name = "name")String name, @RequestParam(value = "id")int id){ return "hello "+ name+"\t"+id; } /** * 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()); //不建议在controller层直接调用addressRepository,虽然同样能够达到效果, //List<Address> addressList2 = addressRepository.queryListByName("%"+name+"%"); //System.out.println(addressList2.toString()); return addressList.toString(); //return "wolaile"; } /** * 查找 * @param id * @return */ @RequestMapping("/query") public String query(@RequestParam(name = "id")int id){ Address a = demoService.findById(id); return a==null?"没有符合查找的东西":a.toString(); } /** * 查找 * @param id * @return */ @RequestMapping("/update") public String query(@RequestParam(name = "id")int id,@RequestParam(name = "address")String address){ Address a = demoService.updateName(id,address); return a==null?"什么也没有":a.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); } /** * 根据ID找到对象 * @param id * @return */ public Address findById(int id){ return addressRepository.findOne(id); } /** * 修改地区名称 * @param id * @param address * @return */ public Address updateName(int id, String address){ Address a = addressRepository.findOne(id); if (a!=null){ a.setAddress(address); addressRepository.save(a); return a; }else return null; } }
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>{ @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