https://github.com/sys1yagi/okhttpmocktest
  
  
    Test the OkHttpClient using MockWebServerRule. 
    https://github.com/sys1yagi/okhttpmocktest
  
        Last synced: 7 months ago 
        JSON representation
    
Test the OkHttpClient using MockWebServerRule.
- Host: GitHub
- URL: https://github.com/sys1yagi/okhttpmocktest
- Owner: sys1yagi
- License: apache-2.0
- Created: 2014-12-27T15:12:42.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-12-27T15:30:27.000Z (almost 11 years ago)
- Last Synced: 2025-02-16T11:45:08.674Z (9 months ago)
- Language: Java
- Size: 234 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
- 
            Metadata Files:
            - Readme: README.md
- License: LICENSE
 
Awesome Lists containing this project
README
          OkHttpMockTest
==============
Test the OkHttpClient using MockWebServerRule.
```java
@RunWith(RobolectricTestRunner.class)
public class ItemObservableTest {
  @Module(injects = ItemObservableTest.class,
  includes = AppModule.class,
  overrides = true)
  class TestModule {
  }
  @Rule
  public MockWebServerRule server = new MockWebServerRule();
  @Inject
  ItemObservable itemObservable;
  @Before
  public void setUp() throws Exception {
    ObjectGraph graph = ObjectGraph.create(
    new TestModule());
    graph.inject(this);
  }
  @Test
  public void testFromId() throws Exception {
    File file = new File("src/androidTest/assets/item.json");
    String json = FileUtils.readFileToString(file);
    MockResponse response = new MockResponse()
    .setResponseCode(200)
    .setBody(json);
    server.enqueue(response);
    itemObservable = spy(itemObservable);
    when(itemObservable.buildPath(anyInt())).thenReturn(server.getUrl("/").toString());
    Item item = itemObservable.fromId(1).toBlocking().single();
    assertThat(item, notNullValue());
    assertThat(item.getId(), is(10));
    assertThat(item.getName(), is("tomato"));
    assertThat(item.getDescription(), is("It is super sweet tomato!"));
    assertThat(item.getPrice(), is(98));
  }
}
```
Take a look at the code for more information :)