+ Start a Discussion
jha.pk5@cloud.comjha.pk5@cloud.com 

Need Test class for Trigger

trigger users on User (before insert, before update) {

    if(Test.isrunningtest()) {
        system.debug('Allowing user create/edit.');
    } else {
        List<User> oUser = [SELECT Id, Security_user__c, ProfileId, UserName FROM User WHERE Id = :System.UserInfo.getUserId() LIMIT 1];
        If(oUser[0].UserName.endsWith('.dev10')){
          
            system.debug('IS DEV USER: Allowing user create/edit.');
        } else {
            Boolean isSecurityUser = oUser[0].Security_user__c;
            if(isSecurityUser) {
              
             
            } else {
                if(Trigger.isUpdate) {
                    List<Profile> list_pAdmin = [SELECT Id FROM Profile WHERE Name IN ('System Administrator','SL: System Administrator','SL: Sys Admin ','SL: Read Only Admin','SL: Read Only Admin Non ') LIMIT 5];
                    Set<String> set_pAdmin = new Set<String>();
                    for(Profile pAdmin : list_pAdmin) {
                        set_pAdmin.add(pAdmin.Id);
                    }
                    for(User oUserRec : Trigger.New) {
                        if((oUserRec.Id == System.UserInfo.getUserId()) && (!set_pAdmin.contains(oUser[0].ProfileId))) {
                            system.debug('IS THE USERS OWN RECORD AND THEY ARE NOT A SYS ADMIN: Allowing user create/edit.');
                        } else {
                            system.debug('IS NOT A TEST, DEV ENVIRONMENT, A SECURITY USER OR THE USERS OWN RECORD: Forbidding user create/edit.');
                            oUserRec .addError('You do not have permission to create or edit user records!');
                        }
                    }
                } else {
                    for(User oUserRec : Trigger.New) {
                        system.debug('IS NOT A TEST, DEV ENVIRONMENT, A SECURITY USER OR THE USERS OWN RECORD: Forbidding user create/edit.');
                        oUserRec .addError('You do not have permission to create or edit user records!');
                    }
                }
            }
        }
    }

}

Best Answer chosen by Admin (Salesforce Developers) 
jhansisridhar_2011jhansisridhar_2011

Hi,

 

code is not working , getting error at void.

 

 

All Answers

AmitSahuAmitSahu

just create an User and insert it in the test mode : 

 

Example : 

 

@isTest

 

public static void testclass()

{

test.start();

User usr= new User(Name='test user',email='test@test.com');

insert usr;

test.Stop();

jha.pk5@cloud.comjha.pk5@cloud.com

hi

 

 

Thank you for your reply but here i am getting 6% code coverage i need to increase 80% Here is the test class

 

 

@isTest

 private class Test{

 static testmethod void testclass()

{

test.startTest();
Profile pUserProfile = [SELECT Id From Profile Where Name = 'System Administrator' Limit 1];  
User usr= new User(LastName='test user',email='test@test.com',Alias = 'testus',
           Emailencodingkey = 'UTF-8',
         Firstname = 'Test',
      Languagelocalekey = 'de',
      Localesidkey = 'de',
      Profileid = pUserProfile.Id,
      isActive = true,
      Timezonesidkey = 'America/New_York',
      Username = 'test@test.com');

insert usr;

test.Stoptest();
} }

jhansisridhar_2011jhansisridhar_2011

Hi,

 

code is not working , getting error at void.

 

 

This was selected as the best answer
AmitSahuAmitSahu

Try this, it will increase a little.... based on the code you have written in the trigger please few more records and update the user record as well and the code coverage wil increase..

 

@isTest

 private class Test{

 static testmethod void testclass()

{

test.startTest();
Profile pUserProfile = [SELECT Id From Profile Where Name = 'System Administrator' Limit 1];  
User usr= new User(LastName='test user',email='test@test.com',Alias = 'testus',
           Emailencodingkey = 'UTF-8',
         Firstname = 'Test',
      Languagelocalekey = 'de',
      Localesidkey = 'de',
      Profileid = pUserProfile.Id,
      isActive = true,
      Timezonesidkey = 'America/New_York',
      Username = 'test@test.com');

insert usr;
update usr;
test.Stoptest();
} }

jha.pk5@cloud.comjha.pk5@cloud.com

Thanks for the reply but still it's not working

SRajesh8SRajesh8
@istest(Seealldata == true)
    private class bepCFeedTest {   
    static testMethod void bepCFeedTest1() {
    Digital_Group__c dg = new Digital_Group__c();
    dg.name='test';
    dg.RecordType__c = 'team';
    insert dg;
    Calendar__c cc = new Calendar__c();
    cc.name ='PubCalendar';
    cc.CalID__c = 'testing';
    insert cc;
    }}