Android Binding and JUnit Notification Check

I want to test my Android models. Especially when the setter should notify about the changes or not.

The view model looks like this (with more related properties):

public class EditViewModel extends BaseObservable {

  private String _comment;
  @Bindable
  public String getComment() {
    return _comment;
  }

  public void setComment(String comment) {
    if (_comment == null && comment == null) {
      // No changes, both NULL
      return;
    }

    if (_comment != null && comment != null && _comment.equals(comment)) {
      //No changes, both equals
      return;
    }

    _comment = comment;
    // Notification of change
    notifyPropertyChanged(BR.comment);
  }
}

In my UnitTest, I register a listener to receive notifications and track them in the following class:

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;
  }
}

So my testing methods are as follows:

@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));
}

If I only perform testing methods, this approach works well. If I run all the tests on one, some fail that the notification was triggered 0. I think statements are called before the callback can be processed.

I have tried the following statements already:

(1) mockito, https://fernandocejas.com/2014/04/08/unit-testing-asynchronous-methods-with-mockito/.

@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());
}

(2) CountDownLatch, SO.

. , ?

+4
1

( GitHub repo ). , . : 100% - EditViewModel:

import android.databinding.Observable;

import org.junit.Test;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

public class EditViewModelTest {

    @Test
    public void setNewNonNullCommentRaisesPropertyChange() {
        // Arrange
        EditViewModel sut = new EditViewModel(null);
        sut.setComment("One");
        Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
        sut.addOnPropertyChangedCallback(listener);
        String newComment = "two";

        // Act
        sut.setComment(newComment);

        // Assert
        verify(listener).onPropertyChanged(sut, BR.comment);
    }

    @Test
    public void setNewNullCommentRaisesPropertyChange() {
        // Arrange
        EditViewModel sut = new EditViewModel(null);
        sut.setComment("One");
        Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
        sut.addOnPropertyChangedCallback(listener);
        String newComment = null;

        // Act
        sut.setComment(newComment);

        // Assert
        verify(listener).onPropertyChanged(sut, BR.comment);
    }

    @Test
    public void setEqualCommentDoesntRaisePropertyChange() {
        // Arrange
        EditViewModel sut = new EditViewModel(null);
        sut.setComment("One");
        Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
        sut.addOnPropertyChangedCallback(listener);
        String newComment = "One";

        // Act
        sut.setComment(newComment);

        // Assert
        verify(listener, never()).onPropertyChanged(sut, BR.comment);
    }

    @Test
    public void setNullToNullDoesntRaisePropertyChange() {
        // Arrange
        EditViewModel sut = new EditViewModel(null);
        sut.setComment(null);
        Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
        sut.addOnPropertyChangedCallback(listener);
        String newComment = null;

        // Act
        sut.setComment(newComment);

        // Assert
        verify(listener, never()).onPropertyChanged(sut, BR.comment);
    }
}

, , , :

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    testCompile "org.mockito:mockito-core:+"

    androidTestCompile "org.mockito:mockito-android:+"       
}

mockito dexmaker . Mockito Maven Central

, unit test test, androidTest - . .

, ViewModel , ViewModel, , , ViewModel.

, . , ViewModel - , - , .

, MyDateFormat unit test - . ViewModel, :

public class UserViewModel extends BaseObservable {

    private final MyDateFormat myDateFormat;

    @Nullable private String name;
    @Nullable private Date birthDate;

    public ProfileViewModel(@NonNull MyDateFormat myDateFormat) {
        this.myDateFormat = myDateFormat;
    }

    @Bindable
    @Nullable
    public String getName() {
        return name;
    }

    @Bindable
    @Nullable 
    public String getBirthDate() {
        return birthDate == null ? null : myDateFormat.format(birthDate.toDate());
    }

    public void setName(@Nullable String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    public void setBirthDate(@Nullable Date birthDate) {
        this.birthDate = birthDate;
        notifyPropertyChanged(BR.birthDate);
    }
}
0

Source: https://habr.com/ru/post/1679836/


All Articles