How are you supposed to use org.junit.matchers.JUnitMatchers.everyItem?

View: New views
6 Messages — Rating Filter:   Alert me  

How are you supposed to use org.junit.matchers.JUnitMatchers.everyItem?

by Alan Burlison-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm trying to use the everyItem method from org.junit.matchers.JUnitMatchers like this:

List<AgreementAndAgreementType> result =
  AgreementAndAgreementTypeDAO.listByUser(c, userId);
assertThat(result, everyItem(hasProperty("userId", equalTo(userId))));

but I'm getting a compile error:

MyTest.java:51: cannot find symbol
symbol  : method assertThat(java.util.List<org.opensolaris.auth.data.AgreementAndAgreementType>,org.hamcrest.Matcher<java.lang.Iterable<java.lang.Object>>)

rewriting that as a loop works fine:

for (AgreementAndAgreementType a : result) {
    assertThat(a, hasProperty("userId", equalTo(userId)));
}

but I'm trying to use everyItem to do the looping instead.  I've clearly not got the usage of everyItem right, but I haven't been able to find any examples of how it is supposed to be used.







Re: How are you supposed to use org.junit.matchers.JUnitMatchers.everyItem?

by Bogdan Mocanu-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



The problem is with everyItem(), because it returns a (not parameterized) Matcher<Iterable<Object>>, which is incompatible with Matcher<T> which is expected (which, finally, is Matcher<Iterable<AgreementAndAgreementType>>).

This way works:
List<AgreementAndAgreementType> result = AgreementAndAgreementTypeDAO.listByUser(c, userId);
assertThat( (List)result, everyItem(hasProperty("userId", equalTo(userId))));

Adding a cast to raw List makes the parameters uniform, therefore allowing for the call.

--- In junit@..., "Alan" <Alan.Burlison@...> wrote:

>
> I'm trying to use the everyItem method from org.junit.matchers.JUnitMatchers like this:
>
> List<AgreementAndAgreementType> result =
>   AgreementAndAgreementTypeDAO.listByUser(c, userId);
> assertThat(result, everyItem(hasProperty("userId", equalTo(userId))));
>
> but I'm getting a compile error:
>
> MyTest.java:51: cannot find symbol
> symbol  : method assertThat(java.util.List<org.opensolaris.auth.data.AgreementAndAgreementType>,org.hamcrest.Matcher<java.lang.Iterable<java.lang.Object>>)
>
> rewriting that as a loop works fine:
>
> for (AgreementAndAgreementType a : result) {
>     assertThat(a, hasProperty("userId", equalTo(userId)));
> }
>
> but I'm trying to use everyItem to do the looping instead.  I've clearly not got the usage of everyItem right, but I haven't been able to find any examples of how it is supposed to be used.
>



Re: How are you supposed to use org.junit.matchers.JUnitMatchers.everyItem?

by Alan Burlison-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> This way works:
> List<AgreementAndAgreementType> result = AgreementAndAgreementTypeDAO.listByUser(c, userId);
> assertThat( (List)result, everyItem(hasProperty("userId", equalTo(userId))));
>
> Adding a cast to raw List makes the parameters uniform, therefore allowing for the call.

Unfortunately it still doesn't seem to work:

cannot find symbol
symbol  : method assertThat(java.util.List,org.hamcrest.Matcher<java.lang.Iterable<java.lang.Object>>)

What does (partially) work is this excrescence:

assertThat((List<Object>)(List)result,
  everyItem(hasProperty("userId", equalTo(userId))));

but that generates an 'unchecked cast' warning.



Re: How are you supposed to use org.junit.matchers.JUnitMatchers.everyItem?

by Bogdan Mocanu-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok, what version of Java do you use? What version of JUnit? Do you have
the Hamcrest library in the classpath?
Are you having the SAME classpath for compile and for runtime?

I suspect that you have one classpath for compile time (like in your IDE
(Eclipse, IDEA, etc)) and a slightly different one during run/test time.

The code below works for me, with onlythe  (List) cast added. The
additional List<Object> cast should be superfluous. But then again, I
also have the unchecked cast warning.


--- In junit@..., "Alan" <Alan.Burlison@...> wrote:
>
> > This way works:
> > List<AgreementAndAgreementType> result =
AgreementAndAgreementTypeDAO.listByUser(c, userId);
> > assertThat( (List)result, everyItem(hasProperty("userId",
equalTo(userId))));
> >
> > Adding a cast to raw List makes the parameters uniform, therefore
allowing for the call.
>
> Unfortunately it still doesn't seem to work:
>
> cannot find symbol
> symbol  : method
assertThat(java.util.List,org.hamcrest.Matcher<java.lang.Iterable<java.l\
ang.Object>>)
>
> What does (partially) work is this excrescence:
>
> assertThat((List<Object>)(List)result,
>   everyItem(hasProperty("userId", equalTo(userId))));
>
> but that generates an 'unchecked cast' warning.
>



Re: How are you supposed to use org.junit.matchers.JUnitMatchers.everyItem?

by Alan Burlison-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Ok, what version of Java do you use?

1.6.0_16

 What version of JUnit?

4.5

> Do you have the Hamcrest library in the classpath?

Yes.

> Are you having the SAME classpath for compile and for runtime?

Yes.

>
> I suspect that you have one classpath for compile time (like in your IDE (Eclipse, IDEA, etc)) and a slightly different one during run/test time.

No, and I'm seeing a compile error.

> The code below works for me, with onlythe  (List) cast added. The
> additional List<Object> cast should be superfluous. But then again, I
> also have the unchecked cast warning.

I notice that the similar hasItem method has a different signature:

static <T> org.hamcrest.Matcher<java.lang.Iterable<T>>
  hasItem(org.hamcrest.Matcher<? extends T> elementMatcher)

The '? extends T' part.
           



Re: Re: How are you supposed to use org.junit.matchers.JUnitMatchers.everyItem?

by David Saff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Looks like the typing on everyItem might be wrong.  Can you open a bug
on github?

Unfortunately, we've found that trying to make the hamcrest typing
play well with both jMock and JUnit has been quite a headache, so I'm
not surprised to hear of problems.

   David Saff

On Thu, Oct 8, 2009 at 8:47 AM, Alan <Alan.Burlison@...> wrote:

>> Ok, what version of Java do you use?
>
> 1.6.0_16
>
>  What version of JUnit?
>
> 4.5
>
>> Do you have the Hamcrest library in the classpath?
>
> Yes.
>
>> Are you having the SAME classpath for compile and for runtime?
>
> Yes.
>
>>
>> I suspect that you have one classpath for compile time (like in your IDE (Eclipse, IDEA, etc)) and a slightly different one during run/test time.
>
> No, and I'm seeing a compile error.
>
>> The code below works for me, with onlythe  (List) cast added. The
>> additional List<Object> cast should be superfluous. But then again, I
>> also have the unchecked cast warning.
>
> I notice that the similar hasItem method has a different signature:
>
> static <T> org.hamcrest.Matcher<java.lang.Iterable<T>>
>  hasItem(org.hamcrest.Matcher<? extends T> elementMatcher)
>
> The '? extends T' part.
>
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>