Since november 2010 you may create test user accounts on Facebook for your application, so that you may test your application without creating dummy accounts and possibly break the Facebook EULA. The test users may be managed using the Facebook Graph API.
The application I was working on, which integrated with Facebook, needed automated integration tests but I could not find any Java API for managing test users. The end result is facebook-test-java-api, a project aimed at testing Facebook application integration.
FacebookTestUserStore facebookStore = new HttpClientFacebookTestUserStore("<appId>", "<appSecret>")); FacebookTestUserAccount account = facebookStore.createTestUser(true, "");
<appSecret> should be replaced with the values from your application.
From the FacebookTestUserAccount, you have access to the different user objects, such as the wall, friends, events etc.
String friendsAsJsonString = account.getFriends();
Assertions may be made on the JSON strings using your favorite toolkit, such as
json-path (Yes Kalle, I’m looking at you. Time to release it!)
You can also let two test users be friends by issuing:
FacebookTestUserStore facebookStore = new HttpClientFacebookTestUserStore("<appId>", "<appSecret>")); FacebookTestUserAccount account1 = facebookStore.createTestUser(true, "read-stream"); FacebookTestUserAccount account2 = facebookStore.createTestUser(true, "read-stream"); account1.makeFriends(account2);
When creating automated tests you probably want to start with a known state for your application. Use the void
deleteAllTestUsers() method on the user store to start with an empty set of users. You can also get a list of available test users from the store by calling the
List<FacebookTestUserAccount> users = facebookStore.getAllTestUsers(); facebookStore.deleteAllTestUsers();
For automated tests integrating with Facebook, the application need the access token for the test user to perform operations on its behalf. Here is an example of how a test case could look like.
FacebookTestUserAccount account = facebookStore.createTestUser(true, "email,read-stream"); systemUnderTest.doStuffIntegratingWithFacebook( account.accessToken() ); assertTrue("Wall did not contain expected data", account.getProfileFeed().contains("stuff posted on wall"));
How to get it
It is available on Maven central, use the following dependency:
<dependency> <groupId>com.jayway.facebooktestjavaapi</groupId> <artifactId>facebook-test-java-api</artifactId> <version>1.0</version> </dependency>
or have a look at the project page for downloads: