Validator类中的依赖关系不在单元测试中初始化

时间:2018-05-01 15:23:24

标签: java spring unit-testing validation spring-boot

来自Spring unit test issue with Validator部分问题已解决。

我正在尝试对Validator类执行单元测试,该类在类中具有依赖项。

@NoArgsConstructor
public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, String> {

    @Autowired
    private AccountService accountService;

    @Override
    public void initialize(final UniqueEmail constraintAnnotation) {
    }

    @Override
    public boolean isValid(final String email, final ConstraintValidatorContext context) {
        return !this.accountService.findByEmail(email).isPresent();
    }
}

以下是 UniqueEmailValidator.java:47 return !this.accountService.findByEmail(email).isPresent();的堆栈

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:177)
    at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:68)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:73)
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:127)
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:120)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:533)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:496)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:465)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:430)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:380)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:169)
    at com.x.x.AccountValidatorTest.shouldDetectDuplicatedEmailAddress(AccountValidatorTest.java:95)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.NullPointerException
    at com.x.x.validator.UniqueEmailValidator.isValid(UniqueEmailValidator.java:47)
    at com.x.x.validator.UniqueEmailValidator.isValid(UniqueEmailValidator.java:1)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:171)
    ... 43 more

我的问题是如果Validator在单元测试中是init,那么如何在单元测试期间提供accountService注入?因为在我看来,accountService没有注入,所以NPE。

@RunWith(SpringRunner.class)
@DataJpaTest
public class AccountValidatorTest {

    private static Validator validator;

    @BeforeClass
    public static void setUp() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
    }

    @Autowired
    private AccountRepository accountRepository;

    @Test
    public void shouldDetectDuplicatedEmailAddress() {

        User user = new User(); 
        // Setters omit

        // accountRepository.save(user);

        Set<ConstraintViolation<AccountRegistrationForm>> violations = validator.validate(user);

        assertEquals(1, violations.size());
    }

}

4 个答案:

答案 0 :(得分:1)

1)如果要为验证器类编写单元测试,请通过模拟所有依赖项来隔离依赖项。

您有两种方式:

  • a)注入UniqueEmailValidator bean,例如:

    @Autowired UniqueEmailValidator UniqueEmailValidator;

使用模拟框架(Mockito很好)来模拟accountService依赖项。

  • b)使用UniqueEmailValidator运算符创建new并使用mockito Runner替换Spring运动员。
    它将加快测试执行速度。

2)如果您想编写集成测试,请注意测试类中使用的@DataJpaTest注释会限制Spring加载主要包含JPA组件的受限上下文。登记/> @DataJpaTest州:

  

可以与之结合使用的注释   @RunWith(SpringRunner.class)进行典型的JPA测试。可以用的时候   测试只关注JPA组件。

并且您的服务不是JPA组件,因此Validator bean中的spring不依赖于依赖关系。

要么@Autowired服务和验证程序,请将服务设置为validator或简化操作:使用@SpringBootTest代替@DataJpaTest

答案 1 :(得分:1)

在David的帮助下,我想我意识到我已将单元测试和集成测试混淆了。所以基本上用单元测试,下面应该是足够的,当然需要更多的测试,但这是想法。

@RunWith(MockitoJUnitRunner.class)
public class AccountValidatorTest {

    private UniqueEmailValidator uniqueEmailValidator;

    @Mock
    private AccountService accountService;

    @Before
    public void setUp() {
        this.uniqueEmailValidator = new UniqueEmailValidator(this.accountService);
    }

    @Test
    public void shouldDetectDuplicatedEmailAddress() {

        // create user object with email "hello@world.com"
        when(accountService.findByEmail(Mockito.anyString())).thenReturn(user);

        boolean violations = uniqueEmailValidator.isValid("hello@world.com", null);

        assertFalse(violations);
    }

    @Test
    public void shouldNotDetectDuplicatedEmailAddress() {
        when(accountService.findByEmail(Mockito.anyString())).thenReturn(Optional.empty());

        boolean violations = uniqueEmailValidator.isValid("hello@world.com", null);

        assertTrue(violations);
    }
}

答案 2 :(得分:0)

我知道这个问题已经很老了,但是我只花了一天的时间并没有找到完整的解释来解决问题的真正方法。

所以我们开始。我将尽我所能详细说明事情。

此过程结束时,您应该具有:

  • 注释界面: UniqueEmail
  • 自定义约束验证器: UniqueEmailValidator
  • 验证程序测试帮助程序配置: ValidatorTestHelperConfig
  • 验证程序测试助手(为清楚起见): ValidatorTestHelper
  • 您的测试类: AccountValidatorTest

这是代码:

@Documented
@Target({TYPE, FIELD, ANNOTATION_TYPE}) 
@Retention(RUNTIME)
@Constraint(validatedBy = UniqueEmailValidator.class)
public @interface UniqueEmail {

    String message() default "{com.x.x.validator.UniqueEmail.message}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

在验证器中,您无需放置 @NoArgsConstructor

public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, String> {

    @Autowired
    private AccountService accountService;

    @Override
    public void initialize(final UniqueEmail constraintAnnotation) { }

    @Override
    public boolean isValid(final String email, final ConstraintValidatorContext context) {
        return !this.accountService.findByEmail(email).isPresent();
     }
}

接下来,您将需要配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class ValidatorTestHelperConfiguration {

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }
}

为进行更清洁的测试,我们编写了一个测试助手(我只输入相关的导入内容)

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { ValidatorTestHelperConfiguration.class })
public abstract class ValidatorTestHelper {

    @Autowired
    protected Validator validator;

    protected List<String> getPropertyPaths(Set<? extends ConstraintViolation<?>> violations) {
        return violations.stream().map(ConstraintViolation::getPropertyPath).map(Object::toString).collect(Collectors.toList());
    }

    protected List<String> getMessageTemplate(Set<? extends ConstraintViolation<?>> violations) {
        return violations.stream().map(ConstraintViolation::getMessageTemplate).map(msg -> msg.replaceAll("([{}])", "")).collect(Collectors.toList());
    }
}

这是最后一块,您的测试。我使用的是JUnit5,因此使用@ExtendWith(请注意,这行不是强制性的)。 请注意,我在这里扩展助手。

import org.junit.jupiter.api.Test;
import javax.validation.ConstraintViolation;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AccountValidatorTest extends ValidatorTestHelper {

    @MockBean
    private AccountService accountService;

    @Test
    public void shouldDetectDuplicatedEmailAddress() {
        User user = new User(); 
        // other things

        Optional<User> userOptional = Optional.of(mock(User.class));
        when(this.accountService.findByEmail(user.getEmail())).thenReturn(userOptional);


        Set<ConstraintViolation<AccountRegistrationForm>> violations = validator.validate(user);

        assertEquals(1, violations.size());

        assertThat(getMessageTemplate(validate)).containsOnlyElementsOf(asList(
            "{com.x.x.validator.UniqueEmail.message}")
        );

        assertThat(getPropertyPaths(validate)).containsExactlyInAnyOrder(
            "accountRegistrationForm.email"
        );
   }
}

就这样,希望对您有所帮助。

答案 3 :(得分:0)

如果你想走纯单元测试的道路,你需要一个自定义的验证器工厂。我会告诉你我之前是如何解决同样的问题的。

问题基本上是您通过调用 Validation.buildDefaultValidatorFactory().getValidator() 获得的 Hibernate 标准 Validator 实现对 Spring 的应用程序上下文一无所知,因此它无法在您的自定义约束验证器中注入依赖项。

在 Spring 应用程序中,Validator 和 ValidatorFactory 接口的实现是类 LocalValidatorFactoryBean,它可以委托给 ApplicationContext 以实例化注入依赖项的约束验证器。

首先要做的是用构造函数注入替换字段注入

public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, String> {

    private final AccountService accountService;
    
    @Autowired
    public UniqueEmailValidator(AccountService accountService) {
        this.accountService = accountService;
    }

    @Override
    public void initialize(final UniqueEmail constraintAnnotation) {
    }

    @Override
    public boolean isValid(final String email, final ConstraintValidatorContext context) {
        return !this.accountService.findByEmail(email).isPresent();
    }
}

你需要做的是

  1. 使用它们的(我认为是模拟的)依赖项实例化您的约束验证器
  2. 创建您自己的 ValidatorFactory,其中包含子弹点 1 中的所有约束验证器
  3. 从这样的工厂实例化你的验证器

这是自定义验证器工厂

public class CustomLocalValidatorFactoryBean extends LocalValidatorFactoryBean {

    private final List<ConstraintValidator<?, ?>> customConstraintValidators;

    public CustomLocalValidatorFactoryBean(List<ConstraintValidator<?, ?>> customConstraintValidators) {
        this.customConstraintValidators = customConstraintValidators;
        setProviderClass(HibernateValidator.class);
        afterPropertiesSet();
    }

    @Override
    protected void postProcessConfiguration(Configuration<?> configuration) {
        super.postProcessConfiguration(configuration);
        ConstraintValidatorFactory defaultConstraintValidatorFactory =
                configuration.getDefaultConstraintValidatorFactory();
        configuration.constraintValidatorFactory(
                new ConstraintValidatorFactory() {
                    @Override
                    public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
                        for (ConstraintValidator<?, ?> constraintValidator : customConstraintValidators) {
                            if (key.equals(constraintValidator.getClass())) //noinspection unchecked
                                return (T) constraintValidator;
                        }
                        return defaultConstraintValidatorFactory.getInstance(key);
                    }

                    @Override
                    public void releaseInstance(ConstraintValidator<?, ?> instance) {
                        defaultConstraintValidatorFactory
                                .releaseInstance(instance);
                    }
                }
        );
    }

}

然后在您的测试课程中,您只需执行以下操作:

class AccountValidatorTest {
    
    private final AccountService mockAccountService = Mockito.mock(AccountService.class);
    private final List<ConstraintValidator<?,?>> customConstraintValidators = 
            Collections.singletonList(new UniqueEmailValidator(mockAccountService));
    private final ValidatorFactory customValidatorFactory = 
            new CustomLocalValidatorFactoryBean(customConstraintValidators);
    private final Validator validator = customValidatorFactory.getValidator();
        
        @Test
        public void shouldDetectDuplicatedEmailAddress() {
        // mock the dependency: Mockito.when(mockAccountService.findByEmail...)
        User user = new User(); 

        Set<ConstraintViolation<?>> violations = validator.validate(user);

        assertEquals(1, violations.size());
    }


}

希望有所帮助。您可以查看我的这篇文章了解更多详情:https://codemadeclear.com/index.php/2021/01/26/how-to-mock-dependencies-when-unit-testing-custom-validators/