title: Hamcrest: test list contains an item that has a private field with a certain value
body: <p>I have this class:</p>
  
  <pre><code>public class A {
      private int x;
  
      public A(int x) {
          this.x = x;
      }
  }
  </code></pre>
  
  <p>And a method in a different class I want to test:</p>
  
  <pre><code>public class B {
      public List underTest() {
          int x = doStuff();
          return Collections.singletonList(new A(x));
      }
  
      private int doStuff() { /* ... */ }
  }
  </code></pre>
  
  <p>I want to test that, at the end of <code>underTest()</code>, the item in the returned list contains an <code>x</code> field equal to a certain value.  I've written this:</p>
  
  <pre><code>import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.Matchers.contains;
  import static org.hamcrest.Matchers.hasProperty;
  import static org.hamcrest.Matchers.is;
  
  @Test
  public void Test1() {
      List result = bInstance.underTest();
      assertThat(result, contains(hasProperty("x", is(1))));
  }
  </code></pre>
  
  <p>But junit complains that <code>item 0: No Property "x"</code> for my test case.</p>
  
  <p>How can I test this? The only thing I can think of is to add a public getter for <code>getX()</code>, then iterate through the returned <code>List</code> and check every element. Remember that, while the method returns a <code>singletonList</code>, the return type is just <code>List</code>, so it <em>could</em> be changed in the future to have multiple values in it.</p>