测试具有大量属性(和子对象)的 bean 的验证

发布于 2025-01-19 09:21:44 字数 1798 浏览 0 评论 0 原文

使用Springboot 2.5.7与捆绑的Junit5(通过Spring-Boot-Starter测试),我正在尝试通过自定义标准注释来测试我在BEAN上放置的约束。

我发现的大多数文档都是关于Junit4的,我找不到使它在Springboot的Junit5中工作的方法。

另外,我有点困惑,因为理想情况下,我想测试每个约束的对照,并且仅找到有关如何在全球测试的文档(使用Junit4)。

有人已经摆弄了这种测试吗?

Just for the sample, here I have a bean for my inbound SQS event:

package com.application.sqs.dto.inbound;

import com.application.sqs.dto.validators.ZoneDateTime;
import lombok.Data;

import java.util.List;

@Data
public class SqsEvent {

    private String version;

    private String id;

    private String source;

    private String account;

    @ZoneDateTime(
            message = "Wrong time."
    )
    private String time;

    private String region;

    private List<String> resources;

    private SqsPayload detail;
}

And an example of a payload (that has lots of child objects, edited):

package com.application.sqs.dto.inbound;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.application.sqs.dto.validators.LocalDate;
import lombok.Data;

import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SqsPayload {
    @JsonProperty("primkey")
    @NotBlank(
            message = "primkey is blank"
    )
    private String primarykey;
    
    @JsonProperty("numb")
    @Pattern(
            regexp="^(A|B)$",
            message = "numb is invalid"
    )
    private String number;

}

I'd like to test that numb only accepts A or B, that primkey must不要空白,我的自定义验证器(@ZonedDateTime)正在按预期工作。

非常感谢您的帮助和指导!

最好的,

Using Springboot 2.5.7 with bundled Junit5 (via spring-boot-starter-test), I'm trying to test the constraints that I have put in place all over the bean via custom an standard annotations.

Most of the documentation I found was about junit4 and I cannot find a way to make it work in springboot's junit5.

Also, I'm a bit puzzled because, ideally, I would like to test contraint per constraint and only found documentation on how to test it globally (using junit4).

Does anyone already fiddled with that kind of testing?

Just for the sample, here I have a bean for my inbound SQS event:

package com.application.sqs.dto.inbound;

import com.application.sqs.dto.validators.ZoneDateTime;
import lombok.Data;

import java.util.List;

@Data
public class SqsEvent {

    private String version;

    private String id;

    private String source;

    private String account;

    @ZoneDateTime(
            message = "Wrong time."
    )
    private String time;

    private String region;

    private List<String> resources;

    private SqsPayload detail;
}

And an example of a payload (that has lots of child objects, edited):

package com.application.sqs.dto.inbound;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.application.sqs.dto.validators.LocalDate;
import lombok.Data;

import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SqsPayload {
    @JsonProperty("primkey")
    @NotBlank(
            message = "primkey is blank"
    )
    private String primarykey;
    
    @JsonProperty("numb")
    @Pattern(
            regexp="^(A|B)
quot;,
            message = "numb is invalid"
    )
    private String number;

}

I'd like to test that numb only accepts A or B, that primkey must not be blank and that my custom validator (@ZonedDateTime) is working as intended for example.

Thanks a lot for your help and guidance!

Best,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦冥 2025-01-26 09:21:45

尚未每个单独的约束,但以下是2种可能性。

选项1

如果您通过Javax验证器运行该类的实例,则每个无效的约束将获得一个约束性violation。这样,您就可以验证每个约束都可以获得所有预期的构造violation。

选项2

定义一个有效的模拟对象,然后在每个测试用例中,您只需将约束字段的值更改为无效案例,并确保它在

程序化Javax验证的程序上的Javax验证详细信息中

,您可以在此处查看在第5步中。

还建议您更深入地了解 javax.validation .Validator 接口上可用的方法。您还拥有一种称为ValidateProperty的方法,用于针对特定属性。

Haven't done per individual constraint, but below are 2 possibilities.

Option 1

If you run an instance of that class through the javax validator, then you will get one ConstraintViolation per invalid constraint. That way, you could just validate that you get all expected ConstaintViolation's for each constraint.

Option 2

define a valid mock object, and then in each test case, you just change a value of a constrained field into an invalid case, and ensure that it catches it

Details on programmatic javax validation

For programmatic javax validation, you could have a look here on step 5.

Also suggest you have a deeper look at the javax.validation.Validator interface on available methods. You also have a method called validateProperty to target specific properties as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文