首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用Junit和Mockito测试Spring验证器

如何用Junit和Mockito测试Spring验证器
EN

Stack Overflow用户
提问于 2015-09-27 16:44:30
回答 2查看 11.9K关注 0票数 1

我有一个Spring Validator:

代码语言:javascript
运行
复制
@Component
public class AddClientAccountValidator implements Validator {

    @Autowired
    private ValidatorUtils validatorUtils;

    @Override
    public boolean supports(Class<?> clazz) {
        return UserDto.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        UserDto user = (UserDto) target;
        validatorUtils.setParam(errors, user);

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "username.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
                "confirmPassword.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "firstName.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "lastName.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "personalId", "personalId.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "city", "city.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "address.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "email.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "phone.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "contribution", "contribution.required");

        validatorUtils.validateAddClientAccount();
    }
}

在上面的验证器已经注入到ValidatorUtils类:

代码语言:javascript
运行
复制
@Component
class ValidatorUtils {

    @Autowired
    private PersonalIdValidator personalIdValidator;
    private Errors errors;
    private UserDto user;

    void setParam(Errors errors, UserDto user) {
        this.errors = errors;
        this.user = user;
    }

    void validateAddClientAccount() {
        validateAccount();
        validContributionAccount();
    }

    private void validateAccount() {
        validUsername();
        validPassword();
        validFirstName();
        validLastName();
        validPersonalId();
        validCity();
        validAddress();
        validEmail();
        validPhone();
    }
    public void validateChangePassword() {
        validChangePassword();
    }

    private void validUsername() {
        if (!user.getUsername().isEmpty()) {
            String username = user.getUsername();
            if ((username.length() < 3) || (username.length() > 40)) {
                errors.rejectValue("username", "username.format");
            }
        }
    }

    private void validPassword() {
        if (!user.getPassword().isEmpty()) {
            String password = user.getPassword();
            if ((password.length() < 3) || (password.length() > 40)) {
                errors.rejectValue("password", "password.format");
            }
            if (!password.equals(user.getConfirmPassword())) {
                errors.rejectValue("confirmPassword", "password.confirm");
            }
        }
    }

    private void validFirstName() {
        if (!user.getFirstName().isEmpty()) {
            String firstName = user.getFirstName();
            String regex = "[A-ZŁ{1}]+[a-zł]+";
            boolean validFirstName = Pattern.matches(regex, firstName);
            if ((firstName.length() < 3) || (firstName.length() > 40) || !validFirstName) {
                errors.rejectValue("firstName", "firstName.format");
            }
        }
    }

    private void validLastName() {
        if (!user.getLastName().isEmpty()) {
            String lastName = user.getLastName();
            String regex = "[A-ZĆŁŚŻŹ{1}]+[a-ząćęłńóśżź]+";
            String regexWithTwoLastName = "[A-ZĆŁŚŻŹ{1}]+[a-ząćęłńóśżź]++[\\s]+[A-ZĆŁŚŻŹ{1}]+[a-ząćęłńóśżź]+";
            boolean validLastName = Pattern.matches(regex, lastName);
            boolean validWithTwoLastName = Pattern.matches(regexWithTwoLastName, lastName);
            if ((lastName.length() < 3) || (lastName.length() > 40)
                    || (!validLastName && !validWithTwoLastName)) {
                errors.rejectValue("lastName", "lastName.format");
            }
        }
    }

这个类有更多的字段验证器,但我跳过了它。

我想使用Junit或最终使用mockito测试我的Validator类。我写这个测试类:

代码语言:javascript
运行
复制
    @RunWith(MockitoJUnitRunner.class)
public class AddClientAccValidatorTest {

    @InjectMocks
    private AddClientAccountValidator validator;
    @Mock
    private ValidatorUtils validatoUtils;
    private UserDto userDto;
    public Errors errors;

    @Before
    public void setUp() {
        UserDto userDto = new UserDto();
        errors = new BeanPropertyBindingResult(userDto, "userDto");
    }

    @Test
    public void testValidate() {
        validator.validate(userDto, errors);
        assertFalse(errors.hasErrors());
    }

}

但是当我运行我的测试时,我得到以下Failet跟踪:

代码语言:javascript
运行
复制
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertFalse(Assert.java:64)
at org.junit.Assert.assertFalse(Assert.java:74)
at pl.piotr.ibank.validator.AddClientAccValidatorTest.testValidate(AddClientAccValidatorTest.java:67)
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

为什么会出现这个错误?我在这一行得到错误:

代码语言:javascript
运行
复制
errors = new BeanPropertyBindingResult(userDto, "userDto");

我的第二个问题是我不能声明多个RunWith注释。当我添加:

代码语言:javascript
运行
复制
@RunWith(MockitoJUnitRunner.class)

我不能使用@RunWith(Parameterized.class)参数化我的测试

如何解决?

有人能帮我吗?也许我的方法不好?用Junit测试Spring Validator的最佳方式是什么?

EN

Stack Overflow用户

发布于 2017-01-02 02:04:23

你不需要模拟任何东西。在验证时,我们需要要验证的对象和错误。创建一个包含要验证的必填字段和errors对象的对象。例如,

代码语言:javascript
运行
复制
@Test
public void shouldReturnErrorsWhenCustomObjectIsNull() {
    CustomValidator customValidator = new CustomValidator();

    Employee employee = new Employee();
    employee.setEmployeeFirstname("empName")


    Errors errors = new BeanPropertyBindingResult(employee, "employee");
    customValidator.validate(employee, errors);
    List<ObjectError> allErrors = errors.getAllErrors();
    assertTrue("Errors list size should not be null : ", allErrors.size() > 0);
    assertTrue(errors.hasErrors());
    assertNotNull( errors.getFieldError("empName") );
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32806118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档