带有休眠验证器的PowerMock错误(JSR-303) [英] PowerMock error with hibernate validator (JSR - 303)

查看:118
本文介绍了带有休眠验证器的PowerMock错误(JSR-303)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用powermock模拟静态方法.我们的代码如下所示:

We are using powermock for mocking static methods. Our code seems as follows

public class ValidationLayer{
    private GenericInputValidator v;
    public ValidationLayer(GenericValidator v){
       this.v = v;
    }
    public boolean isValid(MyObject obj){
       Logger.info(MyFinalClass.staticMethod());
       return v.validate();
    }
}

public class GenericInputValidator{
   private MyOwnValidator validator;
   //setters & getters
   public boolean validate(Object toBeValidated){
      validator.validate(toBeValidated);
   }
}

public class MyOwnValidator(){
   private Validator v;
   public MyOwnValidator(){
     v = Validation.byProvider(HibernateValidator.class).configure()
         .buildValidationFactory().getValidator();
   }
   public validate(){
      //this calls validator method of javax.validation.Validate and analyzes 
      //the result and returns true or false based on the case.
   }
}

My test class looks like following where MyBean annotated with proper JSR-303 annotations

@RunWith(PowerMockRunner.class)
@PrepareForTest({MyFinalClass.class})
public TestClass{
  @Test
  public void testValidationLayer(MyBean bean){
      PowerMock.mockStatic(MyFinalClass.class);
      EasyMock.expect(MyFinalClass.staticMethod()).andReturn("dummy data");
      GenericInputValidator v = new GenericInputValidator(new MyOwnValidator());
      ValidationLayer vLayer = new ValidationLayer(v);
      vLayer.isValid()
  }

}

然后在运行测试时显示以下错误

Then while running tests it is showing the following error

javax.validation.ValidationException : Unable to get available provider resolvers
javax.validation.Validation$ProviderSpecificBootstrapImpl.configure()

深入研究时,我了解到configure()引起了引发异常的问题 当它找不到提供者解析器时(我看到此消息引发了异常 在源代码中).即使我提供了HibernateValidator.class,我也不知道为什么会这样说.
更奇怪的是,如果我不使用powermock来模拟静态方法,即,如果我删除了Logger, 代码,然后一切正常.

On digging deep I understood that configure() is causing problem which is throwing exception when it is unable to find the provide resolvers(I saw the exception throwing with this message in source code ).I don't know why it is saying like that even when I am providing HibernateValidator.class.
More weird thing is if I didn't use powermock to mock static methods i.e., if I remove my Logger code, then everything is working fine.

有什么解决方法吗?

推荐答案

检查PowerMockIgnore批注.它允许您配置哪些类(例如:"javax.*")将不会被模拟代替.

Check the PowerMockIgnore annotation. It allows you to configure which classes (eg: "javax.*") will not be substituted by mocks.

这篇关于带有休眠验证器的PowerMock错误(JSR-303)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆