3

I'm trying to carry out junit test for the Android-DDP library.

To initialize the meteor object, we need a reference to a android context which I'm able to achieve using Robolectric. But the web-sockets is probably talking to the server on a different thread because of which the callback methods are not called and the test methods are getting end.

I used netstat to check if the android client is trying to communicate or not. It shows various ping/pong messages. So, Yes it is trying to talk to the server.

I went through this tutorial as well, Android AsyncTask testing with Android Test Framework. This one tells how to handle the network on UI thread. But nothing seems right.

The sample code, I have worked is:

@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
@RunWith(RobolectricGradleTestRunner.class)
public class MainActivityTest {

    private MainActivity activity;
    private Meteor meteor;
    private String globalUrl = "ws://10.0.3.222:3000/websocket";

    @Before
    public void setup() {

        activity = Robolectric.setupActivity(MainActivity.class);

        meteor = new Meteor(activity, globalUrl);
        meteor.reconnect();

    /*
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    */

    }

    @Test
    public void validateMeteorIsConnected() {

        assertTrue(meteor.isConnected());
    }
}

Any help would be appreciable. Thanks in advance.

Community
  • 1
  • 1
cprakashagr
  • 751
  • 11
  • 28
  • 1
    You have talked about the libraries that you are using, You also talked about what you think is the reason for your problem. You also said how you made sure that the app is talking to the server. But you haven't given any information related to what is your actual test ? Plus you haven't shared your code for the test that is giving you error. One can't just guess what's your problem is when they don't even know what your meteor app is doing ? What your Android app is doing ? Plus if they don't have sections of your code to scrutinize, how can they find the error ? – Ishan Dec 03 '15 at 05:59
  • Hey, I'm carrying out the test for the library and its methods. I have updated the question with my source code. My android app has simple implementation for the library and I have to carry out the simple junit tests for the Android DDP library. – cprakashagr Dec 03 '15 at 06:15
  • Try using MeteorSingleton instead of a new Meteor object. – Ishan Dec 03 '15 at 06:28
  • I have tried the MeteorSingleton as well. No positive signs. – cprakashagr Dec 03 '15 at 06:30

1 Answers1

0

You defined two methods, setup() and validateMeteorIsConnected(), but where are they called?

First, your setup is not correct. After your call to new Meteor(...), you don't need the reconnect() call because the constructor does already establish the connection.

Moreover, you must set up a listener so that you know when the connection has been established or data comes in. This is done with mMeteor.setCallback(...); where the parameter is this or activity.

As you said, the work is done on a different thread and everything is asynchronous.

So you can't just call validateMeteorIsConnected() immediately after connecting.

You need some timer, as shown in the question that you linked to.

caw
  • 30,999
  • 61
  • 181
  • 291
  • I understand `reconnect()` is not needed. Meteor does it on own. Upon `mMeteor.setCallBack(this)`, I'll have to override the methods from the interface as well. Suppose, I append @Test notation before those methods, JUnit will call them the moment I compile them. So, by the time these methods will be called back the test run might end. Some methods like onDataAdded, from the interface, has no guarantee that it will be called back. I tried the delay concept from the link as well. But, there was no good sign on success. Could you please suggest me a sample. – cprakashagr Dec 13 '15 at 16:57
  • What didn't work? What's your code for the timer/delay concept? If you call the constructor `new Meteor(...)`, a connection will be established. If JUnit then calls your `validateMeteorIsConnected()` method after a few seconds, it should really be `true`. If not, the connection could not be established (or it's just taking too long). – caw Dec 13 '15 at 22:02
  • I tried 30 secs delay. Meteor might be taking more than usual time in JUnit. If I am trying to make a connection to meteor from App instead of JUnit, the connection is established very soon. – cprakashagr Dec 14 '15 at 18:34