title: Java unit testing: the easiest way to test if a callback is invoked
body: <p>I often work with methods that accept callbacks, and callbacks seem to be somewhat hard to test. Let's consider the following scenario, if there's a method that accepts a callback with a single method (for simplicity, I assume the testing method is synchronous), the following boilerplate could be written just to ensure that a callback method is invoked:</p>
  
  <pre><code>@Test
  public void testMethod() {
      final boolean[] passed = {false};
      method(new Callback() {
          @Override
          public void handle(boolean isSuccessful) {
              passed[0] = isSuccessful;
          }
      });
      assertTrue(passed[0]);
  }
  </code></pre>
  
  <p>It looks like a surrogate. I would like to know: is there a more elegant way to test such code to make the code above look more like the pseudo-code below?</p>
  
  <pre><code>@Test
  public void testMethod() {
      // nothing explicit here, implicit boolean state provided by a test-runner
      method(new Callback() {
          @Override
          public void handle(boolean isSuccessful) {
              if ( isSuccessful ) {
                  pass(); // not sure how it should look like:
                          // * an inherited method that sets the state to "true"
                          // * or an object with the pass method
                          // * whatever
                          // but doesn't exit testMethod(), just sets the state
              }
          }
      });
      // nothing explicit here too:
      // test runner might check if the state is changed to true
      // otherwise an AssertionError might be thrown at the end of the method implicitly
  }
  </code></pre>
  
  <p>A little cleaner. Is it possible in JUnit, TestNG or any other testing framework? Thanks! </p>
  
  <hr>
  
  <p>UPDATE</p>
  
  <p>Sorry, I seem to have asked a vague question that doesn't really meets what I wanted to ask. I basically meant any code (not necessarily a callback) that might be invoked if certain conditions are satisfied just to <em>set the result state to true</em>. Simply speaking, I just want to get rid of the initial <code>boolean[] passed</code> and the final <code>assertTrue(passed[0])</code> assuming that they are some kind of prologue and epilogue respectively and assuming that the initial state is set to <code>false</code> so the <code>pass()</code> should be invoked to set the state to <code>true</code>. No matter <em>how</em> the <code>passed[0]</code> is set to <code>true</code>, no matter where from. But unfortunately I have asked this question using the context of callbacks, however this is just an option, not a requirement. Thus the title of the question does not reflect what I really wanted to ask, but before the update some answers have been posted.</p>