源码:
@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(),再手动赋值