How to create mock object status in twitter4j?

I use twitter4j and am developing a StatusListener class and need a way to just create a mock Status object so that I can check my class. I do not want to actually connect to the API while I am developing.

Is there a way to create a Status object from a json string? I just want to upload one status from Twitter, save it somewhere as a string, and then reuse it to create a Status object during development.

Can someone tell me how to do this?

+4
source share
3 answers

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

Until you know exactly what the Status object should return, then this will be one of the methods that does not require any connection to the Twitter API.

Say, for example, that we have a YourClass.extractStatusText method that will extract the status text from the Status object and return it.

With Mockito, we can 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 a nice summer day!"); // when - exercise your class String statusText = YourClass.extractStatusText(status); // then - check that the status text is returned assertEquals("It a nice summer day!", statusText); } 
+6
source

Allow to connect and load one status, and then save it through 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

create your own object StatusJSONImpl class constructor and feed with twitter4j.internal.org.json.JSONObject according to the constructor documentation

+2
source

Use the DataObjectFactory.createStatus(String rawJSON) method.

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

+2
source

Source: https://habr.com/ru/post/1369371/


All Articles