您还未登录! 登录 | 注册 | 帮助  

您的位置: 首页 > 软件测试技术 > 功能测试 > 正文

某项目用户修改昵称功能实现及测试

发表于:2017-08-06 作者:我不吃番茄 来源:

  写在前面的话:初入贵圈,菜鸟一枚,分享一些想法,共勉,求轻虐。
  需求:通过restclient插件模拟修改用户信息功能,具体行为:先登录获取用户账户token,通过token找到对应用户信息,并修改用户nickname。PS:部分功能已经封装好,这里正在附录展示直接相关部分;
  app:eclipse(项目框架已搭建好),navicat(查看mySQL数据),restclient插件(用于模拟登录)

  Step1:写代码,如下
  文件:Bumofang/.../MemberController.java
package com.fh.controller.bmf.app;
import ...略 下同
@Controller("AppMemberController")
@RequestMapping(value = "/app/user")
public class MemberController extends AppBaseController {
@RequestMapping(value = { "/update" }, method = {RequestMethod.POST }, produces = {JSON_UTF8})
@ResponseBody
/*login*/
public Object login(@RequestBody Map map) throws Exception {
String mobile = map.get("mobile");
if(StringUtils.isBlank(mobile)){
return ResponseMessageEnum.ARGUMENT_EXCEPTION.toString();
}
PageData pd = new PageData();
pd.put("MOBILE", mobile);
pd = memberService.findByMobile(pd);
if(pd == null || pd.isEmpty()){
return ResponseMessageEnum.ERROR_LOGIN.toString();
}
String userId = pd.getString("ID");
String token = AccessTokenManager.getInstance().putToken(userId);
Map tokenMap = new HashMap();
tokenMap.put("token", token);
return ResponseMessageEnum.SUCCESS.appendMapToString(tokenMap);
}
/*update 修改用户昵称*/
public Object update(@RequestBody Map map) throws Exception{ //Map<>map方法映射用户输入的信息
Object result = getUser();
if(result instanceof ResponseMessageEnum){
return result.toString();
} //通过token获取mySQL里的用户信息,getUser()方法见附录1
PageData pd = (PageData)result; //将用户数据赋值给pd
String nickname = map.get("nickname"); //获取restclient插件中update的数据,即{"nickname":"afterTest"}
pd.put("NICKNAME", nickname); //将{"nickname":"afterTest"}数据传给pd
try {
memberService.edit(pd); //将pd数据反馈给mySQL数据表
}
catch (Exception e) {
return ResponseMessageEnum.SERVER_SQL_ERROR.toString();
}
return ResponseMessageEnum.SUCCESS.appendMapToString(new HashMap());
//在restclient插件返回{“data":{},"code":200,"msg":"success"},appendMapToString()方法见附录4
}
}
  Step2:测试:用restclient插件登录,body参数{"mobile":"15068880000"},返回token值为e59dfcbf7f40662aee9a27f4f0ee8303,如图1,此时对应的mySQL数据如图2
  Step3:测试:用restclient插件模拟update,url:http://localhost:8080/bumofang/app/user/update?token=e59dfcbf7f40662aee9a27f4f0ee8303;body参数{"nickname":"afterTest"},如图3;修改成功后的对应的mySQL数据如图4
  附录1
  文件:Bumofang/.../ AppBaseController.java
package com.fh.controller.base;
import ...
public class AppBaseController extends BaseController{
@Autowired
protected HttpServletRequest request;
@Resource(name = "memberService")
protected MemberService memberService;
protected Object getUser(){
String token = request.getParameter("token");
if(StringUtils.isBlank(token)){
return ResponseMessageEnum.ARGUMENT_TOKEN_EMPTY;
}
AccessToken accessToken = AccessTokenManager.getInstance().getToken(token);
if(accessToken == null){
return ResponseMessageEnum.ARGUMENT_TOKEN_INVALID;
}
String userId = accessToken.getUserId();
PageData pd = new PageData();
pd.put("ID", userId);
try {
PageData rs = memberService.findById(pd);
if(rs == null || rs.isEmpty()){
return ResponseMessageEnum.ERROR_USER_NOT_EXIT;
}
return rs;
} catch (Exception e) {
return ResponseMessageEnum.SERVER_SQL_ERROR;
}
}
}
  附录2:
  文件:Bumofang/.../MemberService.java
package com.fh.service.bmf.member;
@Service("memberService")
public class MemberService {
public void edit(PageData pd)throws Exception{
dao.update("MemberMapper.edit", pd);  // MemberMapper.edit见附录3
}
}
  附录3::
  文件:Bumofang/.../MemberMapper.xml

{C}

update  MEMBER
set
NUMBER = #{NUMBER},
USERNAME = #{USERNAME},
PASSWORD = #{PASSWORD},
NICKNAME = #{NICKNAME},
AVATAR = #{AVATAR},
MOBILE = #{MOBILE},
PROFESSION = #{PROFESSION},
DEFAULT_AREA = #{DEFAULT_AREA},
DEFAULT_ADDRESS = #{DEFAULT_ADDRESS},
REALNAME = #{REALNAME},
ID_CARD = #{ID_CARD},
BANK_CARD = #{BANK_CARD},
STATUS = #{STATUS},
WECHAT_ID = #{WECHAT_ID},
QQ_ID = #{QQ_ID},
WEIBO_ID = #{WEIBO_ID},
ID = ID
where
ID = #{ID}

        附录4:
  文件:Bumofang/.../ResponseMessageEnum.java
package com.fh.common.model;
import ...
public enum ResponseMessageEnum {
public String appendMapToString(Map appendMap) {
Map rsMap = new HashMap();
rsMap.put("data", appendMap);
rsMap.put("code", code);
rsMap.put("msg", this.message);
JSONObject jsonNode = JSONObject.fromObject(rsMap); //将rsMap对象转化为JSON对象,并赋值给jsonNode
return jsonNode.toString();
}
}