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?
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);
}
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
Use the DataObjectFactory.createStatus(String rawJSON)
method.
See http://twitter4j.org/en/javadoc/twitter4j/json/DataObjectFactory.html for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With