title: How to jUnit test the result of code in another thread
body: <p>I have a process that runs in a thread (used as a realtime signal analysis process).  I want to feed that thread process a known input, and then test -- in jUnit -- that the output is correct.   I have a callback listener that can notify me when the thread finishes processing the data and I can run assertions on the result successfully by registering the test case itself as a listener.<br>
     When those assertions fail, they do throw an exception.  But that exception is not registered as a failure by jUnit, presumably because they are happening outside of a test method.  </p>
  
  <p>How do I structure my jUnit test so that the test fails correctly after the listener returns?   Here's a simplified version of the code.  </p>
  
  <pre><code> public class PitchDetectionTest extends TestCase 
      implements EngineRunCompleteListener() {
    AudioData            fixtureData;
    PitchDetectionEngine pitchEngine;
  
    public void setUp() {
      fixtureData = &lt;stuff to load audio data&gt;;
    }
  
    public void testCorrectPitch() {
      pitchEngine = new PitchEngine(fixtureData);
      pitchEngine.setCompletionListener(this);
      pitchEngine.start();   
      // delay termination long enough for the engine to complete
      try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
    }
  
    // gets called by the PitchEngine when it has finished processing all data in the
    // fixtureData.   This is the only method defined by interface    
    // EngineRunCompleteListener.
    public void notifyEngineRunComplete() {
  
      // The real code asserts things about the PitchEngine's results.   When they fail, 
      // an exception is thrown that I can see in the console, but this unit test still  
      // shows as 'success' in the jUnit interface.   What I want is to see 
      // testCorrectPitch() fail.
      assertTrue(false);  
  
    }
  
  }
  
  public class PitchEngine () {
    EngineRunCompleteListener completionListener();
    Thread myThread;
  
    public void start() {
      // other stuff
      myThread = new Thread(this);
      myThread.start();    
    }
  
    public void run() {
      while (some condition) {
        // do stuff with the data
      }
      if (engineRunCompleteListener != null) {
        engineRunCompleteListener.notifyEngineRunComplete();
      }
    }
  
  }
  </code></pre>