我们正在使用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
代码,然后一切正常。
有没有解决方法呢?
答案 0 :(得分:0)
检查PowerMockIgnore注释。它允许您配置哪些类(例如:“javax。*”)不会被模拟替换。