Posted By


d3developer on 08/06/10

Tagged


Statistics


Viewed 110 times
Favorited by 0 user(s)

Related snippets


@reidcarlberg TestUtility


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*
  2. creates entities required for testing.
  3. */
  4.  
  5. public with sharing class TestUtility {
  6.  
  7. /*
  8. A random user is useful for testing in order to be
  9. sure that you are able to see data across posts regardless
  10. of follow status.
  11. */
  12. public static User createTestUser() {
  13. Profile p = [Select Id, Name from Profile where Name = 'System Administrator'];
  14. return TestUtility.createTestUser(p);
  15. }
  16.  
  17. public static User createTestUser(Profile p) {
  18.  
  19. String namePrefix = createRandomWord();
  20.  
  21. AggregateResult[] userCount = [Select count(id) userCount From user where username like :namePrefix];
  22.  
  23. Object users = userCount[0].get('userCount');
  24.  
  25. User testUser = new User();
  26. testUser.Email = '[email protected]';
  27. testUser.Username = namePrefix+users+'@testuser.com';
  28.  
  29. testUser.LastName = 'test';
  30. testUser.Alias = 'test';
  31. testUser.ProfileId = p.Id;
  32. testUser.LanguageLocaleKey = 'en_US';
  33. testUser.LocaleSidKey = 'en_US';
  34. testUser.TimeZoneSidKey = 'America/Chicago';
  35. testUser.EmailEncodingKey = 'UTF-8';
  36. insert testUser;
  37. return testUser;
  38. }
  39.  
  40. public static FeedPost createFeedPost(Id parentId, String title, String body) {
  41. FeedPost fp = new FeedPost();
  42. fp.ParentId = parentId;
  43. fp.Title = title;
  44. fp.Body = body;
  45. insert fp;
  46.  
  47. return fp;
  48.  
  49. }
  50.  
  51.  
  52. /*
  53. Random words are required for testing
  54. as you will likely run into any word I can insert
  55. during a test class and produce false test results.
  56. */
  57.  
  58. public static String createRandomWord() {
  59.  
  60. String ret = 'word' + math.rint(math.random() * 100000);
  61.  
  62. return ret;
  63.  
  64. }
  65.  
  66. //TEST METHODS
  67.  
  68. static TestMethod void testRandomWord() {
  69.  
  70. System.assert(TestUtility.createRandomWord() != TestUtility.createRandomWord());
  71. }
  72.  
  73. static TestMethod void testCreateUser() {
  74.  
  75. User u = TestUtility.createTestUser();
  76.  
  77. System.assert(u.id != null);
  78.  
  79. }
  80.  
  81. static TestMethod void testCreateFeedPost() {
  82.  
  83. User u = TestUtility.createTestUser();
  84.  
  85. FeedPost p = TestUtility.createFeedPost(u.id, 'title', 'body');
  86.  
  87. System.assert(p.id != null);
  88. }
  89.  
  90. /*
  91. Test methods and utilities
  92. */
  93.  
  94.  
  95. /* public static void addSingleKeyword(String kw) {
  96. Chatter_Keywords__c kw1 = new Chatter_Keywords__c();
  97. kw1.Name = kw;
  98. insert kw1;
  99. }*/
  100.  
  101. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.