源码:
@Data
@AllArgsConstructor
public class ImgDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String url;
}
构造ImgDTO对象:
new ImgDTO(url: 'img1')
完整报错信息:groovy.lang.GroovyRuntimeException: failed to invoke constructor: api.dto.ImgDTO(java.lang.String) with arguments: [] reason: java.lang.IllegalArgumentException: wrong number of arguments
构造参数不匹配
分析:
Spock写法new ImgDTO(url: 'img1'),会先调用无参构造函数,再把url: 'img1'赋值给对象ImgDTO,但ImgDTO类并未添加无参构造的注解,所以报错
解决:
方法1、ImgDTO类添加注解:@NoArgsConstructor
方法2、new ImgDTO(),再手动赋值
本文分析了使用Spock语法构造ImgDTO对象时出现的构造参数不匹配错误,并提供了两种解决方案:添加@NoArgsConstructor注解或手动赋值。

4332

被折叠的 条评论
为什么被折叠?



