Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sameBeanAs(...).with("inheritedField", matcher) fails with "inheritedField does not exist" if set to check inherited field #15

Open
kozlovda opened this issue Mar 23, 2015 · 0 comments

Comments

@kozlovda
Copy link

Hi,

The issue above happens when field is inherited from one of super-classes of the tested bean.

This happens due to shallow lookup of the field at BeanFinder.java:33:

    private static Object findBeanAt(List<String> fields, Object object) {
        for (Field field : object.getClass().getDeclaredFields()) {
            field.setAccessible(true);

As per getDeclaredFields() javadoc it doesn't return inherited fields:

This includes public, protected, default (package) access, and private fields, but excludes inherited fields.

Basically, it seems reasonable to update field lookup to search through inherited fields as well (with all field shadowing logic in place).

Thanks in advance for looking into this!

Update

Shadowing is not supported by GSON, so this goes off.

Test case:

import org.junit.Test;

import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class InheritedFieldTest {
    @Test
    public void testBeanMatcherWithDefaultCheck() throws Exception {
        final MyBean bean = new MyBean();

        bean.setOwn("own");
        bean.setInheritedField("inherited");

        final MyBean expected = new MyBean();
        expected.setOwn("own");
        expected.setInheritedField("inherited");

        assertThat(bean, is(sameBeanAs(expected)));
    }

    @Test
    public void testBeanMatcherWithCustomCheck() throws Exception {
        final MyBean bean = new MyBean();

        bean.setOwn("own");
        bean.setInheritedField("inherited");

        final MyBean expected = new MyBean();

        assertThat(bean, is(sameBeanAs(expected)
                .with("own", is("own"))
                .with("inheritedField", is("inherited"))
        ));
    }

    @SuppressWarnings("UnusedDeclaration")
    public static class MyBean extends BaseBean {
        private String own;

        public String getOwn() {
            return own;
        }

        public void setOwn(String own) {
            this.own = own;
        }
    }

    @SuppressWarnings("UnusedDeclaration")
    public static class BaseBean {
        private String inheritedField;

        public String getInheritedField() {
            return inheritedField;
        }

        public void setInheritedField(String inheritedField) {
            this.inheritedField = inheritedField;
        }
    }
}
@kozlovda kozlovda changed the title sameBeanAs(...).with("superclassField", matcher) fails with "superclassField does not exist" sameBeanAs(...).with("inheritedField", matcher) fails with "inheritedField does not exist" if set to check inherited field Mar 23, 2015
nieldw added a commit to nieldw/shazamcrest that referenced this issue Aug 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant