我正在测试我的bean是否有正确的验证注解。我用的是Spring Boot。下面是一个示例测试用例:
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
豆子。
有没有一种简单的方法可以在测试类的上下文中引发异常?
当我对服务接口的方法签名使用验证注解时,一切都按预期工作。我不明白有什么不同。
服务接口:
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;
}
}
服务接口:
@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);
}
}
1条答案
按热度按时间e1xvtsh31#
为什么没有
ConstraintViolationException
在这里长大?bean属性具有@NotNull
注解,bean本身是@Validated
方法签名需要@Valid
豆子。注解本身并不意味着什么,它们应该以某种方式进行处理。在这种情况下
@Validated
spring为其bean处理注解。测试不是springbean,因此框架不会查看与valdidation相关的注解,因此也不例外。即使测试是一个springbean,这种方法也可能无法开箱即用。有关详细信息,请参见此问题。
有没有一种简单的方法可以在测试类的上下文中引发异常?
看看这个问题
当我对服务接口的方法签名使用验证注解时,一切都按预期工作。我不明白有什么不同。
这是因为
service
是 Spring Bean ,但测试不是。当一个方法在service
一旦被调用,就会被MethodValidationInterceptor
,这不是测试的情况