springboot 单元测试

  之前开发测试程序功能时,采取的都是通过连接访问来测试程序,常用的方法有:

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

 

posted on 2018-07-01 20:15  我亦未曾饶岁月  阅读(2975)  评论(0编辑  收藏  举报

导航