PowerMock error with hibernate validator (JSR - 303)

非 Y 不嫁゛ 提交于 2019-12-11 16:33:28

问题


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

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.

Is there any workaround for this?


回答1:


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



来源:https://stackoverflow.com/questions/25307632/powermock-error-with-hibernate-validator-jsr-303

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!