Abdennacer Lachiheb
Abdennacer Lachiheb

Reputation: 4888

Android MVP testing methods that needs network operations

I'm testing my app using Junit and Mackito, testing methods is gone good so far but not with methods that need network operations, how to test them properly, so that the assert is called after fetching data from the backend?

Upvotes: 0

Views: 227

Answers (2)

adalpari
adalpari

Reputation: 3121

Is not a good idea test methods that depend on network calls. Better test backend apart and mock it in client.

Anyways, if you want to wait for the data use: Thread.sleep(Xms) in test method after network call and before your asserts.

@Test
public void testcase() throws InterruptedException {
    // make actions and network call

    // Wait, for example 2 seconds. It depends a lot of connection
    Thread.sleep(2000);

    // Then assert whatever you want
}

Upvotes: 1

Divers
Divers

Reputation: 9569

In general you should test your app, not server side logic. So just mock methods which do network calls.

But, if you want to test network layer of app itself, there is good way, which I recommend, to do that - WireMock

Simply saying it creates locally running webserver which response with predefined responses.

Upvotes: 1

Related Questions