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 9086427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:29:12+00:00 2026-06-16T21:29:12+00:00

I’ve data in mongo which I need to validate: items : [ { item

  • 0

I’ve data in mongo which I need to validate:

"items" : [
            {
                    "item" : 0,
            },
            {
                    "item" : 1,
            }
    ],

There’s a for loop in my code :

for (Object a : getItems()){
 HashMap<?, ?> b = (HashMap<?, ?>)a;
 assertTrue(b.containsValue(0));
}

There is a problem here as items has value 1 and 0 and I need to assert for the second iteration if it contains 1 too. How do I validate if both 1 and 0 are present?

Is there any other assert method which can do this?

Edited:

List lt1 = new ArrayList();
lt1.add(0);
lt1.add(1);
List lt2 = new ArrayList();

for (Object a : getItems()){
 HashMap b = (HashMap)a;
 lt2.add(b.get("item");
}

assertThat(lt2, hasItems(lt1));

This throws an Invocation Target Exception at assertThat.. line.
Also, the JUNIT shows an Assertion Error saying something like below :

Expected : A collection of [0,1]
Got : [0,1]
  • 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-16T21:29:13+00:00Added an answer on June 16, 2026 at 9:29 pm

    Since you are using JUnit, as of v4.4 the library can use the hamcrest matcher library which offers a rich DSL for building test expressions. This means you can remove the loop entirely and write a single assertion, testing for the existence of all expected values.

    For example, hamcrest has a built-in function hasItems() (documentation link for v1.3.RC2 but v1.3 is out – sorry couldn’t find an up to date link).

    import java.util.List;
    import java.util.Arrays;
    import static org.hamcrest.Matchers.hasItems;
    import static org.junit.Assert.assertThat;
    
    @Test
    public void bothValuesShouldBePresent() {
        List<Integer> itemValues = Arrays.asList(new Integer[]{ 0, 1, 2, 3 });
        Integer[] expected = { 0, 1 };
        assertThat(itemValues, hasItems(expected));
    }
    

    This, of course, assumes you can modify your getItems() method to return a simple List<Integer>.

    Finally, depending on the version of JUnit you are using, hamcrest may or may not be bundled. JUnit inlined hamcrest-core between v4.4 and v4.10. Since it was just the hamcrest-core, I explicitly added the hamcrest-all dependency in my projects. As of JUnit v4.11 hamcrest is no longer inlined (much better IMHO) so you will always need to explicitly add the dependency if you want to use the matchers.

    Also, here is a useful blog post on hamcrest collection matching.

    Edit:

    I have tried to think of what your getItems() might return and here is an updated test example. Note that you need to turn the expected value into an array – see Why doesn't this code attempting to use Hamcrest's hasItems compile?

    @Test
    public void bothValuesShouldBePresent() {
        List lt1 = new ArrayList();
        lt1.add(0);
        lt1.add(1);
        List lt2 = new ArrayList();
    
        List fakeGetItems = new ArrayList() {{ add(new HashMap<String, Integer>() {{ put("item", 0); }}); add(new HashMap<String, Integer>() {{ put("item", 1); }} ); }};
    
        for (Object a : fakeGetItems) {
         HashMap b = (HashMap)a;
         lt2.add(b.get("item"));
        }
    
        assertThat(lt2, hasItems(lt1.toArray(new Integer[lt1.size()])));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I

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.