SuppressStaticInitializationFor(Powermock)

我怕爱的太早我们不能终老 提交于 2019-12-07 13:15:28

The documentation for @SuppressStaticInitialization says :-

  • Use this annotation to suppress static initializers (constructors) for one or more classes.

This clearly suggests static initializers and variables havent been mentioned.

But you must take care that you must not pass the class name but you must pass the fully qualified name of class in parameter of @SuppressStaticInitialization

you need to do

@SuppressStaticInitialization(com.myPackage.Employee)
public class Employee{
      .....
}

and you should not be doing this

@SuppressStaticInitialization(Employee.class)
    public class Employee{
          .....
    }

hope you are doing this correctly.

The static variables are also not getting initialized, the way out for this is using the Whitebox class.Steps you need to perform are:-

  • Do the suppression normally
  • use the method Whitebox.setInternalState(ClassName.class,fieldName,fieldValue) to whichever value you want, (inside your test case)
  • Now you wont get a null pointer exception.

hope this helps!

Good luck!

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