mockwebserver

How to mock two APIs with MockWebServer Android test cases

允我心安 提交于 2019-12-04 05:43:27
问题 I am performing instrumentation testing, in that I am invoking one of the activities which call 2 APIs when activity is created. Now I want to write instrumentation test cases for this activity, where I have to mock the API response with mockWebServer of mockito. My mocking code works fine when I call one single API, but it fails when two APIs are getting called simultaneously. Even there is another scenario let's say, we have API to fetch recent message data , but before that, we always

MockWebServer and Retrofit with Callback

我们两清 提交于 2019-12-03 07:25:24
I would like to simulate network communication by MockWebServer. Unfortulatelly retrofit callbacks are never invoking. My code: MockWebServer server = new MockWebServer(); server.enqueue(new MockResponse().setResponseCode(200).setBody("{}")); server.play(); RestAdapter restAdapter = new RestAdapter.Builder().setConverter(new MyGsonConverter(new Gson())) .setEndpoint(server.getUrl("/").toString()).build(); restAdapter.create(SearchService.class).getCount(StringUtils.EMPTY, new Callback<CountContainer>() { @Override public void success(CountContainer countContainer, Response response) { System

Mock HttpResponse with Robolectric

亡梦爱人 提交于 2019-12-03 06:01:42
Using Robolectric 2.3-SNAPSHOT, I want to test an object that'll execute a request in the background. In order to isolate it, I'm trying to mock the HttpResponse returned, without success after some hours invested. I've created a project that anyone can clone. Simly run ./gradlew check https://github.com/Maragues/RobolectricDummyProject (git clone https://github.com/Maragues/RobolectricDummyProject.git ) I've tried Robolectric.setDefaultHttpResponse(200, "my_mocked_word"); MockWebServer ( https://code.google.com/p/mockwebserver/ ) But the tests fail because they query the real URL private

Mock server requests Android Espresso UI Testing

梦想的初衷 提交于 2019-11-30 22:37:58
I am using Espresso to write UI tests for my Android application and would like to mock http requests using MockWebServer. I need to mock authentication responses and sign in the user before the tests are run. Is there a way make the app use mockwebserver so that isntead of making actual requests, I can use respontes enqueued on mockwebserver. So far I have: public class AuthenticationTest { @Rule public ActivityTestRule<Authentication> mActivityTestRule = new ActivityTestRule<>(Authentication.class); private Authentication activity; private MockWebServer server; @Before public void signin()

Square retrofit server mock for testing

南笙酒味 提交于 2019-11-28 02:35:53
What's the best way to mock a server for testing when using the square retrofit framework . Potential ways: Create a new retrofit client and set it in the RestAdapter.Builder().setClient(). This involves parsing the Request object and returning the json as a Response object. Implement this annotated interface as a mock class and use that in place of the version provided by RestAdapter.create() (wont test gson serialisation) ? Ideally I want to have the mocked server provide json responses so I can test the gson serialisation at the same time. Any examples would be greatly appreciated. Victor