Eri*_*-59 5 java validation spring spring-boot
我正在尝试测试我的 bean 是否具有正确的验证注释。我正在使用弹簧引导。这是一个测试用例示例:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
checkIfvalidated()我希望对和 to 的调用setSomeProperty(null)能够引发 a ConstraintViolationException,并且测试能够通过,但它们都失败了:
java.lang.AssertionError:
Expecting code to raise a throwable.
at com.example.sandbox.ValidationTest.test(ValidationTest.java:20)
...
Run Code Online (Sandbox Code Playgroud)
我的 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>
Run Code Online (Sandbox Code Playgroud)
为什么这里没有ConstraintViolationException提出?bean 属性有一个@NotNull注释,bean 本身有注释@Validated,并且方法签名需要一个@Validbean。
有没有一种简单的方法可以在我的测试类的上下文中引发该异常?
当我在服务接口的方法签名上使用验证注释时,一切都按预期工作。我不明白哪里有区别。
服务接口:
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);
}
Run Code Online (Sandbox Code Playgroud)
服务实施:
package com.example.sandbox;
import org.springframework.stereotype.Service;
@Service
public class SomeService implements IService {
@Override
public void setValue(String value) {
// Do nothing
}
}
Run Code Online (Sandbox Code Playgroud)
测试用例:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
==> 测试通过。
根据给定答案的工作代码:
测试类:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
测试的豆子:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
服务接口:
@Validated
public interface TestedBeanService {
// method annotation on the interface method
void setBeanProperty(TestedBean bean, @NotNull String someProperty);
}
Run Code Online (Sandbox Code Playgroud)
服务实施:
@Service
public class TestedBeanServiceImpl implements TestedBeanService {
@Override
public void setBeanProperty(TestedBean bean, String someProperty) {
bean.setSomeProperty(someProperty);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么这里没有
ConstraintViolationException提出?bean 属性有一个@NotNull注释,bean 本身有注释@Validated,并且方法签名需要一个@Validbean。
注释本身没有任何意义,应该以某种方式对其进行处理。在这种情况下,注释由 Spring为其 beans@Validated进行处理。该测试不是 Spring bean,因此框架不会查看与验证相关的注释,因此也不例外。
即使测试是 Spring Bean,该方法也可能无法开箱即用。有关详细信息,请参阅此问题。
有没有一种简单的方法可以在我的测试类的上下文中引发该异常?
看看这个问题
当我在服务接口的方法签名上使用验证注释时,一切都按预期工作。我不明白哪里有区别。
发生这种情况是因为service是 Spring bean,但 test 不是。当调用 上的方法时service,它会被 拦截MethodValidationInterceptor,但测试的情况并非如此
| 归档时间: |
|
| 查看次数: |
4654 次 |
| 最近记录: |