Search code examples
javaunit-testingjunitmockitobeaninfo

Mock BeanInfo class from Object using mockito


Suppose I have the following method structure:

protected static void sub(Object obj, String filler) {

    class cls = obj.getClass();
    BeanInfo beanInfo = Introspector.getBeanInfo(cls);

    // Other code...
}

How do I mock the BeanInfo class given this structure?


Solution

  • Move this logic to separate method:

    static BeanInfo beanInfo(Object obj) {
        Class cls = obj.getClass();
        BeanInfo beanInfo = Introspector.getBeanInfo(cls);
    }
    

    and then mock beanInfo method.