hibernate验证器的PowerMock错误(JSR - 303)



我们使用powermock模拟静态方法。我们的代码如下

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()
  }
}

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

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

深入了解后,我明白configure()导致抛出异常的问题当它无法找到提供解析器时(我看到异常抛出此消息)在源代码中)。我不知道为什么它说,即使当我提供HibernateValidator.class。
更奇怪的是,如果我没有使用powermock来模拟静态方法,也就是说,如果我删除了我的Logger代码,然后一切正常工作。

有什么解决方法吗?

检查PowerMockIgnore注释。它允许您配置哪些类(例如:"javax.*")不会被mock取代。

相关内容

  • 没有找到相关文章

最新更新