简体   繁体   中英

TestNG for Unit testing on Android to test Asynchronous methods

I'm working on Unit testing of a Library for Android platform. We used JUnit, and we find some issue with asynchronous method testing.

Issue - We have a method for ex - Func1() in a library, which is synchronous and it creates a thread which will do interaction with a server and return back a event, and there is an event listener which will listen these events. When I write a test against Func1(), it will return back immediately.But i'm not sure whether it creates a that thread or not.

To resolve this we tried with - Sleep(),CountDownLatch,AsyncTask and other.. We didn't get any result.

Later I found,TetNG will support Async method testing. I integrated TestNG with my eclipse and wrote a sample Test case. But when I try to run Test case by doing - " testcase->right click->run as->testNG Test ", Eclipse showing a error message box saying - "Could't contact the Remote TestNG client.Make sure you dont have older version of testng.jar on your class path ".

So my question is -

Is it possible to use TestNG framework for Unit testing of Android Application / Library? If yes how we can achieve it?

If No, is there any other framework available which is better than jUnit WRT testing on asynchronous methods?

Thanks in advance.

and there is an event listener which will listen these events

JUnit will stop the test as soon as the main thread for the test is over.

Have you tried something like this (there may be some typos):

final CountDownLatch done = new CountDownLatch(1);
final Event expected = ...

Listener lst = new Listener() {
    public void handle event(Event e) {
        assertEquals(expected, e);
        done.countdown();
    }
};
//register listener and run Func1
assertTrue(done.await(1, SECONDS));

That should work without any additional tweakings (if it does not then your listener is probably not doing anything - but in that case you will be able to verify that the test runs for one whole second).

@assylias - Thanks for replay.

It showing same result. Not getting any events from new thread created.

This is what I did.

public void testFunc() throws ... { 
final CountDownLatch done = new CountDownLatch(1);
final Event expected = ABC;

Func(); // which is synchronous and will return immediately; but creates a thread which will do some action  and send an Event.

Listener lst = new Listener() {
    public void handle event(Event e) {
        assertEquals(expected, e);
        done.countdown();
    }
};
done.await();

But test case will wait for indefinate time and no events generated. Will jUnit run child thread or will it kill immediately when its created?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM