+ Start a Discussion
SFAdmin5SFAdmin5 

test class question

Trying to figure out how to write test classes.

 

Gota simple one that just inserts a user record, then checks a list of collaborationgroupmember records to see if that inserted user record is assigned to a specific group.

 

When I run this test I get the following error.  Looke like my list is empty but not sure why.

 

"System.AssertException: Assertion Failed: Expected: 1, Actual: 0"

 

He

@isTest
private class Test_UserTrigger2 {

static testMethod void testChatterAddGroup (){
        Profile profile = [select Id from Profile where name = 'Standard User'];
        User u = new User();
            
                          u.LastName = 'TestLast45';
                          u.FirstName = 'TestFirst45';
                          u.Alias = 'Stest45';
                          u.Email = 'test45@test.com';
                          u.EmailEncodingKey = 'UTF-8';
                          u.LanguageLocaleKey = 'en_US';
                          u.LocaleSidKey = 'en_US';
                          u.CommunityNickname = 'Test45';
                          u.Primary_User_Location__c = 'Waltham';
                          u.TimeZoneSidKey = 'America/Los_Angeles';
                          u.ProfileId= profile.Id;
                          u.Username = 'rosstesterx1234@constantcontact.com';
                          

        test.startTest();        
        insert u;        
        test.stopTest();
        
        List<CollaborationGroupMember> cgm = [SELECT id FROM CollaborationGroupMember WHERE      CollaborationGroup.Name='Waltham' AND MemberId=: u.Id];
        System.assertequals(1,cgm.size());

    }
    
}

 

re's the test that results in that error:

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

no that doesn't matter but thanks a lot for the response anyway.

 

I think I figured this out myself.  Posting the final working code in case anyone else wants to use it.  The purpose of this test class is to test whether a refactored trigger assigns a newly provisioned SF user to a specific chatter group.  the trigger and class code are readily available on these boards and bob buzzard's blog, but a working test is not available out there

 

    static testMethod void testChatterAddGroup (){
        Profile profile = [select Id from Profile where name = 'Standard User'];
        
        
        List<User> userList = new List<User>{};
        
            for(Integer i = 0; i < 200; i++){
                User u = new User (FirstName = 'Joe' + i);
                          u.LastName = 'Smith' + i;
                          u.Email = 'jjsmith@constantcontact.com' + i;
                          u.EmailEncodingKey = 'UTF-8';
                          u.LanguageLocaleKey = 'en_US';
                          u.LocaleSidKey = 'en_US';
                          u.CommunityNickname = 'Jjsmith' + i;
                          u.Primary_User_Location__c = 'Waltham';
                          u.TimeZoneSidKey = 'America/Los_Angeles';
                          u.ProfileId= profile.Id;
                          u.Alias = 'Jsmith';
                          u.Username = 'jjsmith@constantcontact.com' + i;            
            
                userList.add(u);
            }                       

        test.startTest();        
        insert userList;        
        test.stopTest();

        List<CollaborationGroupMember> cgm = [SELECT id FROM CollaborationGroupMember 
                                                WHERE CollaborationGroup.Name='Waltham' AND MemberId IN : userList];
            for (CollaborationGroupMember m: cgm ){
                System.assertequals(200,cgm.size());

            }

    }

 

All Answers

colemabcolemab

Could it be that you have an extra space in you query?

 

Should it be:

 

List<CollaborationGroupMember> cgm = [SELECT id FROM CollaborationGroupMember WHERE      CollaborationGroup.Name='Waltham' AND MemberId= :u.Id];

Just a thought . . .

SFAdmin5SFAdmin5

no that doesn't matter but thanks a lot for the response anyway.

 

I think I figured this out myself.  Posting the final working code in case anyone else wants to use it.  The purpose of this test class is to test whether a refactored trigger assigns a newly provisioned SF user to a specific chatter group.  the trigger and class code are readily available on these boards and bob buzzard's blog, but a working test is not available out there

 

    static testMethod void testChatterAddGroup (){
        Profile profile = [select Id from Profile where name = 'Standard User'];
        
        
        List<User> userList = new List<User>{};
        
            for(Integer i = 0; i < 200; i++){
                User u = new User (FirstName = 'Joe' + i);
                          u.LastName = 'Smith' + i;
                          u.Email = 'jjsmith@constantcontact.com' + i;
                          u.EmailEncodingKey = 'UTF-8';
                          u.LanguageLocaleKey = 'en_US';
                          u.LocaleSidKey = 'en_US';
                          u.CommunityNickname = 'Jjsmith' + i;
                          u.Primary_User_Location__c = 'Waltham';
                          u.TimeZoneSidKey = 'America/Los_Angeles';
                          u.ProfileId= profile.Id;
                          u.Alias = 'Jsmith';
                          u.Username = 'jjsmith@constantcontact.com' + i;            
            
                userList.add(u);
            }                       

        test.startTest();        
        insert userList;        
        test.stopTest();

        List<CollaborationGroupMember> cgm = [SELECT id FROM CollaborationGroupMember 
                                                WHERE CollaborationGroup.Name='Waltham' AND MemberId IN : userList];
            for (CollaborationGroupMember m: cgm ){
                System.assertequals(200,cgm.size());

            }

    }

 

This was selected as the best answer