包装异常>>>测试

一个包装异常的小例子.

public Activity updateActivity(Map Mapactivity) {
Long id = Long.valueOf(Mapactivity.get("id").toString());
Activity act = (Activity)this.getHibernateTemplate().get(Activity.class,id);
try {
BeanUtils.populate(act,Mapactivity);
this.getHibernateTemplate().update(act);
}catch(Exception e){
throw new DolphinOperationException(e,CodeCategories.UPDATE_FAILURE); //CodeCategories.UPDATE_FAILURE 常量200或者400
}
return act;
}

1.因为BeanUtils本身就会需要抛出异常,但所有的异常都是Exception
2.这个例子,需求是更新一个拼单,这里使用到了助手类(临时发现,如果是Date类型为空则会出现异常,使用需注意),在更新完拼单后将这个拼单返回,并告知更新成功或者失败.

下面是这个异常包装类

public class DolphinOperationException extends RuntimeException {
private Integer errorCode; //定义一个错误编码

public DolphinOperationException(Integer errorCode) {
this.errorCode = errorCode;
}

public DolphinOperationException(Throwable cause, Integer errorCode) {
super(cause);
this.errorCode = errorCode;
}

public DolphinOperationException(Integer errorCode,String message) {
super(message);
}

public DolphinOperationException(String message, Throwable cause, Integer errorCode) {
super(message, cause);
this.errorCode = errorCode;
}

public Integer getErrorCode() {
return errorCode;
}
}

继承RuntimeException,重写了构造方法

测试类

@Test
@Rollback(false)
public void updateActivity(){
Address ads = new Address();
ads.setAddress("22222");

User user = new User();
user.setId(15l);

Activity act = new Activity();
act.setId(2l);
act.setAddress(ads);
act.setStartTime(new Date());
act.setTitle("光棍节写测试1");
Activity activity=am.updateActivity(act);
assertNotNull(activity);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值