title: JUnit best practices
body: <p>I am writing an in-memory implementation of Queue Services (similar to Amazon SQS) for local  testing.</p>
  
  <p>By specification, a pull() call on non-empty queue doesn't have to return a message. In case of just one consumer, my In-Memory implementation will always return a message. So, the first test case (correct in terms of SQS specification) is obviously useless and would pass even with an empty pull() implementation always returning null:</p>
  
  <pre><code>@Test
  public void testPushAndPullBySpec() {//useless
      QueueService qs = new InMemoryQueueService();
  
      qs.push("TEST_MSG_1");
  
      Message message = qs.pull();
      Assert.assertTrue(message == null || message.getBody().equals("TEST_MSG_1"));
      if (message != null) {
          qs.delete(message.getReceiptHandle());
      }
  }
  </code></pre>
  
  <p>This implementation looks meaningful, but it actually contradicts the specification and cannot be used with other implementations:</p>
  
  <pre><code>@Test
  public void testPushAndPullByImpl() {
      QueueService qs = new InMemoryQueueService();//could fail if another implementation was used
  
      qs.push("TEST_MSG_1");
  
      Message message = qs.pull();
      Assert.assertTrue(message.getBody().equals("TEST_MSG_1"));
      qs.delete(message.getReceiptHandle());
  }
  </code></pre>
  
  <p>So, my only question is: how do I write my unit tests correctly in this situation?</p>