Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7786641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:27:00+00:00 2026-06-01T20:27:00+00:00

I need to test method which returns ordered List of some complex objects. Simplified

  • 0

I need to test method which returns ordered List of some complex objects. Simplified example:

class MyObject {
    public String foo() { return someString; }
}

I want to test both: orderable of returned collection (since now I was using org.hamcrest.collection.IsIterableContainingInOrder.contains and fulfiling predicate).

To sum up. I’m looking for such syntax:

@Test
public void shouldMatchPredicate() {
    List<MyObject> collection = testObject.generate();
    //collection = [myObject#x, myObject#y, myObject#z]
    assertThat(collection, somePredicate("x", "y", "z")
}

Default one, contains method is not working, since first argument is Collection<MyObject> and arguments in predicate are Strings. I need some kind of bridge between it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:27:01+00:00Added an answer on June 1, 2026 at 8:27 pm

    Since Predicate is a Guava object and Hamcrest does not depend on Guava it will not have a Matcher that will take a Predicate. Also, since Guava is not dependent on Hamcrest, they will not provide a Matcher either.

    I suggest writing your own Matcher that takes a Predicate. This is relatively easy to do. Get the source code for IsIterableContainingInOrder and modify it to take a Predicate.

    Another option would be to do the following:

    assertThat(Iterables.all(myList, myPredicate), CoreMatchers.is(true));
    

    This won’t give you much documentation on a failure but it will pass/fail properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to simulate a test scenario in which I call the getBytes() method
I need to test some api. For example I have multiple @Test methods in
I need to unit test a method that returns a type I can't easily
I need a test for a variable which would evaluate to true in two
I need to test my Android app for playing flash objects in the WebView.
I have a method which is used to check if the given class is
How do I set up my test method on that mocks a repository which
What is the correct way to unit test a method that returns a sequence
I tried to make an unit test for this method which is pretty simple
in my unit test framework, for some of the messages ( which are simply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.