Asserting there is an object with a certain property value in a collection of objects?

997 views
Skip to first unread message

Christian Balzer

unread,
Oct 24, 2014, 8:02:38 AM10/24/14
to hamcre...@googlegroups.com

Hi,


This is a cross-post from StackOverflow; I didn't realize in time there was a dedicated user group...
The original post can be found here: http://stackoverflow.com/q/26546618/2018047

I just started using Hamcrest, so I'm probably doing it all wrong.

I have a List<Foo> foos and the Foo interface looks a bit like this:

public abstract interface Foo {

  public String getBar();

}

It is implemented by impl.FooImpl:

public class FooImpl implements Foo {

  protected String _bar;

  public String getBar() {
    return _bar;
  }

}

My assert looks like this:

assertThat(
  foos, 
  Matchers.hasItem(
    Matchers.<Foo> hasProperty(
      "bar", 
      equalTo(Whitebox.<String> getInternalState(AnotherClass.class,"A_FIELD_NAME"))
    )
  )
);

Unfortunately, JUnit/Hamcrest isn't happy:

java.lang.AssertionError: 
Expected: a collection containing hasProperty("_bar", "someValue")
     got: <[com.example.impl.FooImpl@2c78bc3b]>

Any idea what I need to do to fix this?

Kind regards,

Christian

Colin Vipurs

unread,
Oct 24, 2014, 8:21:33 AM10/24/14
to hamcre...@googlegroups.com
What version of Hamcrest are you running?

I've just tried this on 1.3 and it works as expect for both passing and failing cases.

--
You received this message because you are subscribed to the Google Groups "Hamcrest Java Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hamcrest-jav...@googlegroups.com.
To post to this group, send email to hamcre...@googlegroups.com.
Visit this group at http://groups.google.com/group/hamcrest-java.
For more options, visit https://groups.google.com/d/optout.



--
Maybe she awoke to see the roommate's boyfriend swinging from the chandelier wearing a boar's head.

Something which you, I, and everyone else would call "Tuesday", of course.

Christian Balzer

unread,
Oct 24, 2014, 8:36:07 AM10/24/14
to hamcre...@googlegroups.com
I've got hamcrest-all-1.1.jar on my path, apparently...

Colin Vipurs

unread,
Oct 24, 2014, 8:40:08 AM10/24/14
to hamcre...@googlegroups.com
if you can, move to 1.3.  You'll also want to check your junit (and possible other framework) versions as this brings in it's own version of hamcrest in some incarnations.  Hamcrest is usually the first thing in my list of dependencies as this always causes such a pain

Steve Freeman

unread,
Oct 24, 2014, 9:20:44 AM10/24/14
to hamcre...@googlegroups.com
I would certainly upgrade but, in this case, it's not complaining about Hamcrest itself.

What is all that Whitebox stuff? Shouldn't you just be passing in a string to compare?

S

Christian Balzer

unread,
Oct 24, 2014, 10:30:47 AM10/24/14
to hamcre...@googlegroups.com
I just tried the simplified version, without PowerMock's Whitebox but instead with hamcrest-all-1-3.jar.
Here is the test code:

public class FooTest {

    public static void main(String... args) {
        ArrayList foos = new ArrayList();
        foos.add(new FooImpl());

        assertThat(
                foos, 
                Matchers.hasItem(
                  Matchers.<Foo> hasProperty(
                    "bar", 
                    equalTo("some value")
                  )
                )
              );
    }
}

Eclipse now complains that "The method assertThat(T, Matcher<? super T>) in the type MatcherAssert is not applicable for the arguments (ArrayList, Matcher<Iterable<? super Foo>>)"...
Colin, could you please share what exactly you are doing?
Is the problem Foo vs. FooImpl? (It looks like the problem is that Iterable isn't compatible with Matcher<ArrayList>, but I won't pretend I really understand what's going on right now...)
I'm sure I'm missing something really super simple, but right now I probably can't see the forest for the trees...

Brgrds,

Christian

Colin Vipurs

unread,
Oct 24, 2014, 10:51:03 AM10/24/14
to hamcre...@googlegroups.com
This is the code I used:

       public void findsItemInList() throws Exception {
List<FooI> foos = Arrays.<FooI>asList(new Foo());
assertThat(foos, Matchers.<Foo>hasItem(
Matchers.hasProperty("bar", equalTo("bar"))

));
}

You'll also want to check the correct imports for Hamcrest if you're accidentally pulling it in from elsewhere:

Your issue is supplying the type value for hasItem.


Christian Balzer

unread,
Oct 24, 2014, 12:00:01 PM10/24/14
to hamcre...@googlegroups.com
I just tried your code (just changed FooI to Foo), but get the same error message. When I place my cursor on the import statement, and jump to the declaration (F3), Eclipse takes me to the right jar file - so I assume it's not that.

I'll try again over the weekend, but for now it doesn't seem to fix the issue... :'(

List<Foo> foos = Arrays.<Foo> asList(new FooImpl());
assertThat(foos, Matchers.<Foo> hasItem(Matchers.hasProperty("bar", equalTo("some value"))));


Exception in thread "main" java.lang.AssertionError: 
Expected: a collection containing hasProperty("bar", "some value")
     got: <[com.example.chris.impl.FooImpl@18e2b22]>

No more luck with assertThat(foos, Matchers.<FooImpl> :-(

Have a nice weekend,

Christian

Christian Balzer

unread,
Oct 27, 2014, 11:08:08 AM10/27/14
to hamcre...@googlegroups.com
I just got this resolved thanks to StackOverflow, your help, and a bit of experience with Spring:

So, my actual property is called _FooBar, the getter is called getFooBar(), and the right matcher isMatchers.equalTo( "fooBar", "some value")... (Note the LOWERCASE "f".)

Thanks for your help!

Brgrds,
Christian 
Reply all
Reply to author
Forward
0 new messages