November 4, 2017

Tinkering with Twitter: Setting up an API Testing environment with Twitter4J

This is Part Three of a multi-part blog series on putting together a basic API test framework for the Twitter Search API. Care to go back to the beginning

The last few posts we covered a lot of ground:

For this entry, we will be walking a user through setting up an API development environment with IntelliJ, Java, and Twitter4J.



Step One: Download and Install IntelliJ IDEA


Step Two: Set Up a New Project

  • Run IntelliJ IDEA. The main splash screen should appear.

The IntelliJ IDEA splash screen


  • From the splash screen, select "Create Project".
  • This time, let's create a  Maven project. Select "Maven" in the left-hand menu, make sure that Java 1.8 is selected, and hit "Next".

Select Maven, the Java 1.8 SDK, and choose "Next". 
  • What do you want to use as a group id? Since I have my website "tjmaher.com", I used that as a package name: com.tjmaher. 
  • What do you want to call the project? I selected TinkeringWithTwitter as a name. Select "Next". 

Enter a group id, artifact id, and keep the default snapshot version. Select "Next". 

  • Select "Finish" on the next screen. 
  • Your Java project is now set up!

Step Three: Set up the Twitter4J Dependencies in the POM.Xml file
  • Review the "Maven Integration" section on the main page for Twitter4J on http://twitter4j.org/en/index.html
  • Go to the pom.xml file that was created automatically in the root folder of the project.
  • If it is not there already, after the group id, artifact id, and version, add the dependency block, placing the group id, artifact id, and version for Twitter4J inside it. It will look something like this: 
   <dependencies>  
     <dependency>  
       <groupId>org.twitter4j</groupId>  
       <artifactId>twitter4j-core</artifactId>  
       <version>[4.0,)</version>  
     </dependency>  
     <dependency>  
       <groupId>org.testng</groupId>  
       <artifactId>testng</artifactId>  
       <version>6.8</version>  
       <scope>test</scope>  
     </dependency>  
   </dependencies>  

  • The Twitter4J Java Library should automatically download. 
  • We will include the test framework TestNG.

Step Four: Set up the Twitter credentials for Twitter4J to use

  • Once you are at the root folder of the project, create a new file by going to the horizontal menu, and select File -> New -> File.
  • Create a new file "twitter4j.properties".
  • Copy and paste the following code into the twitter4j.properties:
 debug=true  
 oauth.consumerKey=****************  
 oauth.consumerSecret=****************  
 oauth.accessToken=****************  
 oauth.accessTokenSecret=****************  

  • Did you create a new Twitter account, create a Twitter app, and get credentials that this project could use? If not, follow the instructions.
  • Cut-and-paste the consumer key, consumer secret, access token, and access token secret into the file, replacing the asterisks. 

... Now, our project is all set! Next, we will work through posting Tweets to your Twitter account using Twitter4J!



Happy Testing!



-T.J. Maher
Twitter | LinkedIn | GitHub

// Sr. QA Engineer, Software Engineer in Test, Software Tester since 1996.
// Contributing Writer for TechBeacon.
// "Looking to move away from manual QA? Follow Adventures in Automation on Facebook!"
Post a Comment