( 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() {
EditViewModel sut = new EditViewModel(null);
sut.setComment("One");
Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
sut.addOnPropertyChangedCallback(listener);
String newComment = "two";
sut.setComment(newComment);
verify(listener).onPropertyChanged(sut, BR.comment);
}
@Test
public void setNewNullCommentRaisesPropertyChange() {
EditViewModel sut = new EditViewModel(null);
sut.setComment("One");
Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
sut.addOnPropertyChangedCallback(listener);
String newComment = null;
sut.setComment(newComment);
verify(listener).onPropertyChanged(sut, BR.comment);
}
@Test
public void setEqualCommentDoesntRaisePropertyChange() {
EditViewModel sut = new EditViewModel(null);
sut.setComment("One");
Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
sut.addOnPropertyChangedCallback(listener);
String newComment = "One";
sut.setComment(newComment);
verify(listener, never()).onPropertyChanged(sut, BR.comment);
}
@Test
public void setNullToNullDoesntRaisePropertyChange() {
EditViewModel sut = new EditViewModel(null);
sut.setComment(null);
Observable.OnPropertyChangedCallback listener = mock(Observable.OnPropertyChangedCallback.class);
sut.addOnPropertyChangedCallback(listener);
String newComment = null;
sut.setComment(newComment);
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);
}
}