Created
January 2, 2020 12:19
-
-
Save sahoosunilkumar/6d2adf44c3b6a131994e4e9c1c6e698d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sunilsahoo.sonarqube.validator; | |
import androidx.lifecycle.LiveData; | |
import androidx.lifecycle.MediatorLiveData; | |
import androidx.lifecycle.ViewModel; | |
public class HomeViewModel extends ViewModel { | |
private MediatorLiveData<String> liveData = new MediatorLiveData<>(); | |
public LiveData<String> getLiveData() { | |
return liveData; | |
} | |
public void execute() { | |
liveData.postValue("Loading"); | |
//perform other operation | |
liveData.postValue("Success"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sunilsahoo.sonarqube.validator; | |
import androidx.lifecycle.MediatorLiveData; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.mockito.InjectMocks; | |
import org.mockito.Mock; | |
import org.mockito.MockitoAnnotations; | |
import static org.mockito.BDDMockito.then; | |
import static org.mockito.BDDMockito.willDoNothing; | |
import static org.mockito.BDDMockito.willReturn; | |
import static org.mockito.Mockito.anyString; | |
import static org.mockito.Mockito.spy; | |
import static org.mockito.Mockito.times; | |
public class HomeViewModelTest { | |
@Mock | |
private MediatorLiveData<String> liveData; | |
@InjectMocks | |
private HomeViewModel homeViewModel; | |
@Before | |
public void setUp() { | |
MockitoAnnotations.initMocks(this); | |
homeViewModel = spy(homeViewModel); | |
willReturn(liveData).given(homeViewModel).getLiveData(); | |
willDoNothing().given(liveData).postValue(anyString()); | |
} | |
@Test | |
public void testExecute() throws Exception { | |
homeViewModel.execute(); | |
then(liveData).should(times(1)).postValue("Loading"); | |
then(liveData).should(times(1)).postValue("Success"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment