java - 如何在带有验证注释的 bean 属性的测试用例中引发 ConstraintValidationException?

标签 java spring spring-boot validation

我正在尝试测试我的 bean 是否具有正确的验证注释。我正在使用 Spring 启动。这是一个示例测试用例:

package com.example.sandbox;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import javax.validation.ConstraintViolationException;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.validation.annotation.Validated;

@SpringBootTest
class ValidationTest {

    @Test
    void testConstructor() {
        TestedBean bean = new TestedBean(null);

        assertThatThrownBy(() -> checkIfValidated(bean)).isInstanceOf(ConstraintViolationException.class);
    }

    @Test
    void testSetter() {
        TestedBean bean = new TestedBean(null);

        assertThatThrownBy(() -> bean.setSomeProperty(null)).isInstanceOf(ConstraintViolationException.class);
    }

    private void checkIfValidated(@Valid TestedBean bean) {

    }

    @Validated
    class TestedBean {
        @NotNull
        private String someProperty;

        public TestedBean(String someProperty) {
            super();
            this.someProperty = someProperty;
        }

        public String getSomeProperty() {
            return someProperty;
        }

        public void setSomeProperty(@NotNull String someProperty) {
            this.someProperty = someProperty;
        }
    }
}

我希望调用 checkIfvalidated()setSomeProperty(null) 引发 ConstraintViolationException,并通过测试,但是他们都失败了:

java.lang.AssertionError: 
Expecting code to raise a throwable.
    at com.example.sandbox.ValidationTest.test(ValidationTest.java:20)
    ...

我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>com.example.springbootsandbox</artifactId>
    <version>0.0</version>
    <name>SpringBootSandbox</name>
    <description>Sandbox for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.5.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

为什么这里没有引发ConstraintViolationException? bean 属性有一个 @NotNull 注释,bean 本身是 @Validated 并且方法签名需要一个 @Valid bean。

有没有一种简单的方法可以在我的测试类上下文中引发该异常?

当我在服务接口(interface)的方法签名上使用验证注解时,一切都按预期进行。我不明白哪里不一样。

服务接口(interface):

package com.example.sandbox;

import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;

@Validated
public interface IService {
    public void setValue(@NotNull String value);
}

服务实现:

package com.example.sandbox;

import org.springframework.stereotype.Service;

@Service
public class SomeService implements IService {
    @Override
    public void setValue(String value) {
        // Do nothing
    }
}

测试用例:

package com.example.sandbox;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import javax.validation.ConstraintViolationException;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SomeServiceTests {
    @Autowired
    IService service;

    @Test
    void testSetValue() {
        assertThatThrownBy(() -> service.setValue(null)).isInstanceOf(ConstraintViolationException.class);
    }
}

==> 测试通过。


根据给定答案的工作代码:

测试类:

@SpringBootTest
class ValidationTest {
    @Autowired
    private Validator validator; // Using the default validator to test property annotations

    @Autowired
    private TestedBeanService service; // Using a service to test method annotations

    @Test
    void testPropertyAnnotations() {
        TestedBean bean = new TestedBean(null);
        Set<ConstraintViolation<TestedBean>> violations = validator.validate(bean);
        assertThat(violations).isNotEmpty();
    }

    @Test
    void testMethodAnnotations() {
        TestedBean bean = new TestedBean(null);
        assertThatThrownBy(() -> service.setBeanProperty(bean, null)).isInstanceOf(ConstraintViolationException.class);
    }
}

被测bean:

@Validated
class TestedBean {
    @NotNull
    private String someProperty;

    public TestedBean(String someProperty) {
        super();
        this.someProperty = someProperty;
    }

    public String getSomeProperty() {
        return someProperty;
    }

    public void setSomeProperty(String someProperty) { // No more annotation on setter
        this.someProperty = someProperty;
    }
}

服务接口(interface):

@Validated
public interface TestedBeanService {
    // method annotation on the interface method
    void setBeanProperty(TestedBean bean, @NotNull String someProperty);
}

服务实现:

@Service
public class TestedBeanServiceImpl implements TestedBeanService {

    @Override
    public void setBeanProperty(TestedBean bean, String someProperty) {
        bean.setSomeProperty(someProperty);
    }
}

最佳答案

Why is there no ConstraintViolationException raised here? The bean property has a @NotNull annotation, the bean itself is @Validated and the method signature requires a @Valid bean.

注解本身没有任何意义,应该以某种方式对其进行处理。在这种情况下,@Validated 注释由 Spring 为其 bean 处理。测试不是 Spring bean,因此框架不会查看与验证相关的注释,因此也不异常(exception)。

即使测试是一个 Spring Bean,该方法也可能无法开箱即用。参见 this询问详情。

Is there a simple way to have that exception raised in the context of my test class?

看看this问题

When I use validation annotations on method signatures for a service interface, everything works as expected. I don't understand where is the difference.

发生这种情况是因为 service 是一个 Spring bean,但 test 不是。当调用 service 上的方法时,它会被 MethodValidationInterceptor 拦截,这不是测试的情况

关于java - 如何在带有验证注释的 bean 属性的测试用例中引发 ConstraintValidationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65427838/

相关文章:

java - Java中的映射数据结构

java - 如何自定义 Sonar 从 pom.xml 生成的 PMD 配置文件?

java - spring中哪个类实现了JpaRepository接口(interface)

web-services - CXF jaxws 端点相对发布地址

java - 每个 spring 源代码 ide 的 springframework.org/tags 表单的 Maven 引用?

java - Thymeleaf EL 表达式生成链接为/?param=value 而不是/value

java - 如何使用带有 boolean 值的 If 语句并读取用户输入

spring - spring boot项目中的多个Web应用程序

Spring Boot - 多个模板位置

java - JPA + StoredProcedureCall + 对象类型 IN 参数