Skip to content

Instantly share code, notes, and snippets.

@kmdupr33
Created May 29, 2015 10:43
Show Gist options
  • Select an option

  • Save kmdupr33/f3462dc4bb1cc76ddac5 to your computer and use it in GitHub Desktop.

Select an option

Save kmdupr33/f3462dc4bb1cc76ddac5 to your computer and use it in GitHub Desktop.
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import android.content.SharedPreferences;
@RunWith(MockitoJUnitRunner.class)
public class UnitTestSample {
private static final String FAKE_STRING = "HELLO WORLD";
@Mock
Context mMockContext;
@Test
public void readStringFromContext_LocalizedString() {
// Given a mocked Context injected into the object under test...
when(mMockContext.getString(R.string.hello_word))
.thenReturn(FAKE_STRING);
ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);
// ...when the string is returned from the object under test...
String result = myObjectUnderTest.getHelloWorldString();
// ...then the result should be the expected one.
assertThat(result, is(FAKE_STRING));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment