title: How to test that a created value is unique?
body: <p>In my Java class I have a method that creates an object and implements certain logic to assign random values to some of its variables, using one static instance of Random (<code>static Random rn = new Random();</code>). One of these variables is <code>objectId</code>. </p>
  
  <p>How can I write <strong>Spock</strong> unit test to verify that <code>objectId</code> value is <strong>unique among all created objects</strong>?  </p>
  
  <p>If I needed it just for <strong>two consecutive objects</strong> I would write something like this (omitting imports):</p>
  
  <pre><code>class MyTest extends Specification {
  
      def @Shared obj = new MyObject()
  
      def "consecutive IDs are not equal"() {
          given:
              def id1 = ar.randomObject(MyObject.rn).getId()
              def id2 = ar.randomObject(MyObject.rn).getId()
          expect:
              id1 != id2
  
      }
  } 
  </code></pre>
  
  <p>How to extend this logic to a variable number of instances?</p>