I am performing functional tests for my project in java.
PubNub is not making the callbacks when I try to subscribe or publish within the test method even though the connection is fine.
If I try to publish or subscribe to the channel from a separate java client, the callback works well.
public class FunctionalTest {
private Logger logger = LoggerFactory.getLogger(FunctionalTest.class);
@Test
public void verifyCreateSuccess() throws JAXBException,IOException {
Pubnub pubnub = new Pubnub("publisher-key", "subscriber-key");
Callback callback = new Callback() {
public void successCallback(String channel, Object response) {
logger.info("PUBLISHER::" + response.toString());
}
public void errorCallback(String channel, PubnubError error) {
logger.info("PUBLISHER::" + error.toString());
}
};
try {
pubnub.publish("foo-test",new JSONObject().put("test", 1),callback);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Using PubNub inside Java Unit Test
The JUnit test are completing before the Pubnub call completes. Put a sleep of say 60 seconds in one of the test cases and see if you get a publish (not that it is taking 60 seconds, just want to make sure you pause for enough time).
When you call publish the method returns immediately but the work is queued up, which is why we use callback, the test exits, the pubnub object falls out of scope and the callback never fires.
This really isn’t a PubNub issue as much as attempting to write a functional test with a unit test framework using an asynchronous framework like PubNub.