从0开始学习接口自动化测试(二)

从0开始学习接口自动化测试(二)

感兴趣的小伙伴可以加入QQ群:556029945

开发一个简单的接口demo供自己练习

 这里我们选用springboot来开发一个简单的接口demo,因为springboot够轻量,开发接口速度非常之快,建议对开发技术感兴趣的小伙伴们可以学习一下。

 相关技术的讲解这不再做详细说明,如果有对详细技术感兴趣的可以加群了解或翻阅springboot官方文档进行学习,这里只附上部分代码和思路供大家参考。(以下接口例子是极简单demo,不含实际业务)

 介绍一下接口的背景,就是一个查询和写入个人资料的接口,没有严格的业务逻辑,但实际测试中与功能测试相差不大,只是在于场景设计,数据准备这块会复杂一些。另外就是,接口开发工具是采用idea。

 1.首先安装jdk环境,maven环境,git环境,以及idea开发工具,这里不做具体展开如何搭建,请大家自行百度了解。

 2.idea中新建一个maven工程,也可以直接通过spring的模板来创建功能,这样可以自己勾选到导入的依赖。

 3.在pom中添加关于springboot的依赖

    
      org.springframework.boot
      spring-boot-starter-jdbc
      2.1.0.RELEASE
    
    
      org.springframework.boot
      spring-boot-starter-web
      2.1.0.RELEASE
    
    
      org.springframework.boot
      spring-boot-autoconfigure
      2.1.0.RELEASE
    

 4.先建api类,没有严格遵循对象转化设计这块,除了写入数据库,所有对象都采用api类进行数据转换。



package com.dsh.rest.api;

/**
 * profile
 * @Author dsh
 */
public class Profile {


  /**
   * 姓名
   */
  private String name;
  /**
   * 年龄
   */
  private String age;
  /**
   * 出生日期
   */
  private String birthday;
  /**
   * 自我介绍
   */
  private String selfIntroduction;
  /**
   * 婚姻状况
   */
  private String marriageStatus;
  /**
   * 联系信息
   */
  private Contact contact;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAge() {
    return age;
  }

  public void setAge(String age) {
    this.age = age;
  }

  public String getBirthday() {
    return birthday;
  }

  public void setBirthday(String birthday) {
    this.birthday = birthday;
  }

  public String getSelfIntroduction() {
    return selfIntroduction;
  }

  public void setSelfIntroduction(String selfIntroduction) {
    this.selfIntroduction = selfIntroduction;
  }

  public Contact getContact() {
    return contact;
  }

  public void setContact(Contact contact) {
    this.contact = contact;
  }

  public String getMarriageStatus() {
    return marriageStatus;
  }

  public void setMarriageStatus(String marriageStatus) {
    this.marriageStatus = marriageStatus;
  }

  public void setId(String id) {
  }
}

package com.dsh.rest.api;

public class Contact {

  /**
   * 联系方式
   */
  private String phoneNumber;
  /**
   * 地址
   */
  private String address;
  /**
   * 电子邮件
   */
  private String email;
  /**
   * 紧急联系人
   */
  private String emergencyContact;
  /**
   * 紧急联系人联系方式
   */
  private String emergencyContactPhoneNumber;
  /**
   * 紧急联系人地址
   */
  private String emergencyContactAddress;

  public String getPhoneNumber() {
    return phoneNumber;
  }

  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getEmergencyContact() {
    return emergencyContact;
  }

  public void setEmergencyContact(String emergencyContact) {
    this.emergencyContact = emergencyContact;
  }

  public String getEmergencyContactPhoneNumber() {
    return emergencyContactPhoneNumber;
  }

  public void setEmergencyContactPhoneNumber(String emergencyContactPhoneNumber) {
    this.emergencyContactPhoneNumber = emergencyContactPhoneNumber;
  }

  public String getEmergencyContactAddress() {
    return emergencyContactAddress;
  }

  public void setEmergencyContactAddress(String emergencyContactAddress) {
    this.emergencyContactAddress = emergencyContactAddress;
  }
}

package com.dsh.rest.api;

public class ProfileResponse {

  /**
   * 结果
   */
  private String result;
  /**
   * ID号
   */
  private String id;

  public String getResult() {
    return result;
  }

  public void setResult(String result) {
    this.result = result;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

}

 5.资源类(一个查询,一个写入)


package com.dsh.rest.resource;

import com.dsh.rest.api.Profile;
import com.dsh.rest.service.ProfileQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/profile")
public class ProfileQueryResource {

  @Autowired
  private ProfileQueryService profileQueryService;

  @GetMapping(value = "/query", consumes = "application/json; charset=utf-8")
  public Profile query(@RequestParam(value = "name") String name){
    return profileQueryService.query(name);
  }

}

package com.dsh.rest.resource;

import com.dsh.rest.api.Profile;
import com.dsh.rest.api.ProfileResponse;
import com.dsh.rest.service.ProfileRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/profile")
public class ProfileRegisterResource {

  @Autowired
  private ProfileRegisterService profileRegisterService;

  @PostMapping(value = "/record", consumes = "application/json; charset=utf-8")
  public ProfileResponse recordMyselfInfo(@RequestBody Profile myselfInfo){
    String id = profileRegisterService.save(myselfInfo);
    ProfileResponse profileResponse = new ProfileResponse();
    if (null != id){
      profileResponse.setResult("success");
      profileResponse.setId(id);
    }
    return profileResponse;
  }

}

 6. 服务层(先定义接口,再实现,无业务逻辑所以很简单的一层)


package com.dsh.rest.service;

import com.dsh.rest.api.Profile;

public interface ProfileQueryService {

  Profile query(String name);

}

package com.dsh.rest.service.impl;

import com.dsh.rest.api.Profile;
import com.dsh.rest.repository.ProfileRepository;
import com.dsh.rest.service.ProfileQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProfileQueryServiceImpl implements ProfileQueryService {

  @Autowired
  private ProfileRepository selfInfoDao;


  @Override
  public Profile query(String name) {
    return selfInfoDao.query(name);
  }
}

package com.dsh.rest.service;

import com.dsh.rest.api.Profile;

public interface ProfileRegisterService {

  String save(Profile profile);

}

package com.dsh.rest.service.impl;

import com.dsh.rest.api.Profile;
import com.dsh.rest.repository.ProfileRepository;
import com.dsh.rest.service.ProfileRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProfileRegisterServiceImpl implements ProfileRegisterService {

  @Autowired
  private ProfileRepository selfInfoDao;

  @Override
  public String save(Profile profile) {
    return selfInfoDao.addProfile(profile);
  }
}

 

以免篇幅过长,导致有的小伙伴看的累,而中途放弃治疗,我们在这里就截断,需要了解更多的,可以看下一篇。

posted @ 2019-04-03 09:47 Sean_Deng 阅读(...) 评论(...) 编辑 收藏

你可能感兴趣的:(接口测试)