0

I have been trying in vain and everytime I am just reaching a dead end.

I am trying to look for a way to stub network responses with my json similar to cy.intercept in cypress using mockwebserver and espresso

This is my code

 @Test
    fun loginwithincorrectpassword(){
        mockWebServer.enqueue(MockResponse().setBody("{\"success\":false,\"message\":\"Your emccail or password is incorrect. Use ‘Forgot Passddword’ option below to reset\",\"errorCode\":1019}"))
        val intent = Intent()
        activityRule.launchActivity(intent)
        mainActivityTest.bypassPushnotification()
        mainActivityTest.bypassWalkthrough()
        onView(withId(R.id.login_pass_continue_btn)).perform(click())
        onView(withText("Your email or password is incorrect. Use ‘Forgot Password’ option below to reset")).check(matches(isDisplayed()))
        onView(withText("OK")).check(matches(isDisplayed())).perform(click())
        onView(withId(R.id.login_pass_password_edt)).perform(clearText())



    }


class Login {
    val mainActivityTest = MainActivityTest()
    val mockWebServer = MockWebServer()
    @Before
    @Throws(IOException::class, InterruptedException::class)
    fun setup() {
        mockWebServer.start(8080)
  

    }

 @Rule
    @JvmField
    var activityRule = ActivityTestRule<SplashActivity>(
            SplashActivity::class.java, true, false

    )

I am not sure if I am going on the right direction, I had referred to several materials but kind of dumb in not getting it. Need help in proceeding forward.

Emjey
  • 2,038
  • 3
  • 18
  • 33
  • Can you provide information about what happens when you run the provided code? Does it give you an error? Does it succeed where it should fail? What response do you actually see? – agoff Jul 07 '21 at 13:42
  • Hey @agoff . So i am expecting the text that I am keeping it in the setbody in the code but it shows me the actual text that the backend sends. There is no error in running test – Emjey Jul 07 '21 at 14:31
  • So i am not really sure what i am I missing in that – Emjey Jul 07 '21 at 14:31
  • I am not sure how do I mock a specific endpoint, like when you click on button there are several endpoints that get called and I want to be mocking specific which I want to. I feel I am not doing it the right way – Emjey Jul 07 '21 at 14:42
  • I am following this article https://levelup.gitconnected.com/android-ui-test-with-mocked-api-7b8e11f5499c – Emjey Jul 08 '21 at 03:58
  • Are you positive that your mockwebserver is running? Is that entire `@Before` block getting executed? Because you haven't provided the full scope of the class, I'm not able to really tell. Also, from your more recent comment, I'd look up that specific question instead of this more general one -- look for something that mocks a specific endpoint using mockwebserver. – agoff Jul 08 '21 at 13:25
  • @agoff: Can you suggest any tutorial for wiremock? I feel I am missing something fundamental here as the output is the same. The tests run but endpoints don't get mocked. I tried adding logs at every function that i am calling in the process including the intercept method. The stubfor does return the json response. I feel either I am missing on the base url or in the gradlefile for wiremock to properly point to. – Emjey Jul 08 '21 at 15:22
  • For WIreMock or MockWebServer? They are two different technologies. – agoff Jul 08 '21 at 16:07
  • For mockwebserver – Emjey Jul 08 '21 at 16:16
  • @agoff So finally i was able to get set with the things now the app is pointing to the localhost url but i am not able to see any response returned by the mockserver, however the error i see is : <-- HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to 127.0.0.1 not permitted by network security policy – Emjey Jul 09 '21 at 11:57
  • I do have `clearTextpermitted` in the manifest set as `true` – Emjey Jul 09 '21 at 11:58
  • 1
    damn! it worked. i had to add it in the network_security_layer too – Emjey Jul 09 '21 at 12:23
  • 1
    Glad you were able to get it all sorted, apologies on not getting you any documentation. Would you mind posting what you were able to do to fix the problem as a solution? It'll make others searching for similar issues more likely to look at your question. – agoff Jul 09 '21 at 13:39
  • Hey agoff! I did not do anything much except for changing the urls in the build.gradle and gradle.properties. a big blunder I was doing is i was making the changes in one variant and i was running an other variant crying it isn't working! Only later I realised that an other variant is getting installed – Emjey Jul 12 '21 at 01:15
  • And i had filters over the logs so some of the loggers would not appear becauze of the filters and when i cleared them i was able to see why the response was not sent by mockwebserver because of the CLEARTEXT restrictions which got fixed by adding true in the network_security_layer – Emjey Jul 12 '21 at 01:16
  • But right now, is there way to point the build base url to 127.0.0.1 without having to change it in the gradle? Changing it in the gradle is making me mock the entire list of api the app uses. However I do want to mock only the APIs which I am testing. So even If i am pointing it to the test environment, when i am running a scenario is there a way to point the build to this url at the test file or in the run time? So i can only mock in the scenario I want to and not everything – Emjey Jul 12 '21 at 01:19
  • I know that this involves making changes to the gradle, but could you just add mock as an environment variant to your app? So then you'd have a certain gradle command (`./gradlew connected{buildEnv}{flavor}AndroidTest`) to run the tests? – agoff Jul 12 '21 at 13:40
  • Yes but this would be no different than adding it in the gradle. Like in the web platform, i have the privilege to run it in the staging environment and at the same time mock with intercept at the run time the end point I want to mock and let the other APIs point to the staging env. Sadly at mobile since it builds once , I guess it's either all mock(127.0.0.1) or all production server(staging) – Emjey Jul 12 '21 at 15:03

0 Answers0