Wednesday 22 July 2015

Test class Factory design pattern

Test class Factory design pattern

public class UtileTesClass {



    /*-----setup data for user----*/
    public Static User setupUser()
    {
        Profile prof = [select id from profile where name = 'System Administrator'];
        User user = new User(alias = 'standt', email = 'my123_123@gmail.com', emailencodingkey = 'UTF-8',
                         lastname = 'Test', languagelocalekey = 'en_US', localesidkey = 'en_US',
                         profileid = prof.Id, timezonesidkey = 'America/Los_Angeles',
                         username = 'testuser_123@gmail.com');
        insert user;
        return user;
    }

   
    /*----------------Setup data for Account----*/
    public static Account setupAccount(user MyMnger)
    {      
        Account  acc= new Account();  
        acc.Name='Test Account';
        acc.ShippingStreet ='Royal Street';
        acc.ShippingCity='London';
        acc.ShippingState='UK';
        acc.ShippingPostalCode ='123456';
        acc.ShippingCountry='UK';
        return acc;
    }

     
    /*----------------Setup data for Case ----*/
    public static case setupCase(Account acc)
    {      
        Case cs = new Case();
        cs.status='Open';
        cs.Subject='Demo Case';
        cs.Origin='Email';
        cs.AccountId = acc.id;
        cs.Reason = 'Defects';  
        return cs;
    }
   
       
}

------------------------------------------------- Test class --------------------------------------

@isTest(SeeAllData=false)

public class TestclassFactory{

    static testmethod void testCCREscalations (){
    User CCRUser =  UtileTesClass.setupUser();

    Test.startTest();

        system.runAs(CCRUser) {
        List<user> selectedusers = new List<User>();
        String CommonError ='';
        Integer ErrorFlag = 0;

         

            // setup Account
            Account account = UtileTesClass.setupAccount(CCRUser);
            insert account;
           
            List<account> listAccount = [select id,name from Account];
            System.assertEquals(1, listAccount.size());

            Case cas = UtileTesClass.setupCase(account);
            insert cas;          

        }
    Test.stopTest();
    }                

 
    }