title: Android binding and JUnit testing of notification
body: <p>I want to test my Android view models. Especially when the setter should notify about changes or not.</p>
  
  <p>The view model looks like this (with more bindable properties):</p>
  
  <pre class="lang-java prettyprint-override"><code>public class EditViewModel extends BaseObservable {
  
    private String _comment;
    @Bindable
    public String getComment() {
      return _comment;
    }
  
    public void setComment(String comment) {
      if (_comment == null &amp;&amp; comment == null) {
        // No changes, both NULL
        return;
      }
  
      if (_comment != null &amp;&amp; comment != null &amp;&amp; _comment.equals(comment)) {
        //No changes, both equals
        return;
      }
  
      _comment = comment;
      // Notification of change
      notifyPropertyChanged(BR.comment);
    }
  }
  </code></pre>
  
  <p>In my UnitTest I register a listener, to get the notifications and track them with a following class:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class TestCounter {
    private int _counter = 0;
    private int _fieldId = -1;
  
    public void increment(){
      _counter++;
    }
  
    public int getCounter(){
      return _counter;
    }
  
    public void setFieldId(int fieldId){
      _fieldId = fieldId;
    }
  
    public int getFieldId(){
      return _fieldId;
    }
  }
  </code></pre>
  
  <p>So my test methods looks like the following:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Test
  public void setComment_RaisePropertyChange() {
    // Arrange
    EditViewModel sut = new EditViewModel(null);
    sut.setComment("One");
    final TestCounter pauseCounter = new TestCounter();
    // -- Change listener
    sut.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
      @Override
      public void onPropertyChanged(Observable sender, int propertyId) {
        pauseCounter.increment();
        pauseCounter.setFieldId(propertyId);
      }
    });
    String newComment = "two";
  
    // Act
    sut.setComment(newComment);
  
    // Assert
    assertThat(pauseCounter.getCounter(), is(1));
    assertThat(pauseCounter.getFieldId(), is(BR.comment));
    assertThat(sut.getComment(), is(newComment));
  }
  </code></pre>
  
  <p>If I execute the test methods alone, this approach works well. If I execute all tests at one, some fail, that the notification was <code>0</code> times called. I think, that the assertions is called before the callback could be handled.</p>
  
  <p>I tried already following approches:</p>
  
  <p>(1) Mock the listener with mockito as described in <a href="https://fernandocejas.com/2014/04/08/unit-testing-asynchronous-methods-with-mockito/" rel="nofollow noreferrer">https://fernandocejas.com/2014/04/08/unit-testing-asynchronous-methods-with-mockito/</a>.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Test
  public void setComment_RaisePropertyChange() {  
    // Arrange
    EditViewModel sut = new EditViewModel(null);
    sut.setComment("One");
    Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
  
    // -- Change listener
    sut.addOnPropertyChangedCallback(listener);
    String newComment = "two";
  
    // Act
    sut.setComment(newComment);
  
    // Assert
    verify(listener, timeout(500).times(1)).onPropertyChanged(any(Observable.class), anyInt());
  }
  </code></pre>
  
  <p>(2) Tried to use <code>CountDownLatch</code> as described in several SO answers.</p>
  
  <p>None of them helped me. What can I do to be able to test the binding notification?</p>