Loading ...
Sorry, an error occurred while loading the content.

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

Expand Messages
  • Alan
    I m trying to use the everyItem method from org.junit.matchers.JUnitMatchers like this: List result =
    Message 1 of 6 , Oct 7, 2009
    View Source
    • 0 Attachment
      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.
    • Bogdan
      The problem is with everyItem(), because it returns a (not parameterized) Matcher , which is incompatible with Matcher which is expected
      Message 2 of 6 , Oct 7, 2009
      View Source
      • 0 Attachment
        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@yahoogroups.com, "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.
        >
      • Alan
        ... Unfortunately it still doesn t seem to work: cannot find symbol symbol : method
        Message 3 of 6 , Oct 8, 2009
        View Source
        • 0 Attachment
          > 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.
        • Bogdan
          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
          Message 4 of 6 , Oct 8, 2009
          View Source
          • 0 Attachment
            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@yahoogroups.com, "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.
            >
          • Alan
            ... 1.6.0_16 What version of JUnit? 4.5 ... Yes. ... Yes. ... No, and I m seeing a compile error. ... I notice that the similar hasItem method has a different
            Message 5 of 6 , Oct 8, 2009
            View Source
            • 0 Attachment
              > 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.
            • David Saff
              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
              Message 6 of 6 , Oct 23, 2009
              View Source
              • 0 Attachment
                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
                >
                >
                >
                >
              Your message has been successfully submitted and would be delivered to recipients shortly.