Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create mock Status object in twitter4j?

I am using twitter4j and developing StatusListener class and need a way to just create a mock Status object so I can test my class. I don't want to have to actually connect to the API while I am developing.

Is there a way to create a Status object from json string? I just want to download one status from Twitter, save it somewhere as a string and then reuse it to create Status object while I'm developing.

Can someone tell me how to do this?

like image 385
Dmitri Avatar asked Aug 28 '11 12:08

Dmitri


3 Answers

One option is to actually create a mock Status object using a mock testing framework like Mockito.

As long as you know exactly what the Status object should return, then this would be one method which would not require any connection to the Twitter API.

Let's say for example that we have a YourClass.extractStatusText method which will extract the status text from a Status object and return that.

With Mockito, we could do the following:

import static org.mockito.Mockito.mock;

// ...

public void testCode() {
  // given - we'll mock a Status which returns a canned result:
  Status status = mock(Status.class);
  when(status.getText()).thenReturn("It's a nice summer day!");

  // when - exercise your class
  String statusText = YourClass.extractStatusText(status);

  // then - check that the status text is returned
  assertEquals("It's a nice summer day!", statusText);
}
like image 98
coobird Avatar answered Oct 23 '22 09:10

coobird


Let connect and download one status, then save it via Serializing

http://java.sun.com/developer/technicalArticles/Programming/serialization/
http://twitter4j.org/en/javadoc/twitter4j/Status.html
http://twitter4j.org/en/javadoc/twitter4j/StatusJSONImpl.html

to create own Object use StatusJSONImpl class and feed constructor with twitter4j.internal.org.json.JSONObject according to constructor documentation

like image 26
Marek Sebera Avatar answered Oct 23 '22 10:10

Marek Sebera


Use the DataObjectFactory.createStatus(String rawJSON) method.

See http://twitter4j.org/en/javadoc/twitter4j/json/DataObjectFactory.html for details.

like image 35
user1095283 Avatar answered Oct 23 '22 09:10

user1095283