function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zoom_VZoom_V 

Test code help needed

I have rewritten a class which originally had its test code within the actual class. I am now trying to move this test code into its own class and am getting some errors. 

 

Whenever I run this test I get an error saying "Attempting to de-reference a null object". 


Here is the full code : 

I have rewritten a class which originally had its test code within the actual class. I am now trying to move this test code into its own class and am getting some errors.

Whenever I run this test I get an error saying "Attempting to de-reference a null object".


Here is the full code :

@isTest
private class TestCSHOrgModelQuarterly {
static Id getRecordTypeId(String sObjectType, String Name) {
return [select id from recordtype where sobjecttype=:sObjectType and name=:Name limit 1].id;
}
static final Id A_MM_Rectype = getRecordTypeId('Organization_Model__c','Global');

User User1;
Organization_Model__c Org1;
//CSH_New__c csh1, csh2, cshErr;
String u; // Unique number for this test run.

TestCSHOrgModelQuarterly() {

// Create a unique string for this test so we're never confused with existing data.
u = Datetime.now().millisecond().format();

// Create the two users:
Profile p = [select id from profile where name='Standard User'];



User1 = new User(LastName=u+'Test2', email='test2@test.com', CompanyName='BCD Testing', Country='USAISTAN', Department='Sales', Title='Tester 2', alias='test2',
username='test2@test.com', emailencodingkey='UTF-8', languagelocalekey='en_US', localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles',
Division='North America', isActive = True);
insert User1;


Org1 = new Organization_Model__c(Name=u+'Test2', recordtypeid=A_MM_Rectype, Org_Model_CSH_to_be_completed_by__c = User1.id, Org_Model_CSH__c = 'Quarterly');
insert Org1;



//killtime();
// Note: Satisfied comes *after* unhappy, so that's the one we should be cloning.
}

static testMethod void testReview() {
// Reduce the number of message we get during debugging.
system.debug(Logginglevel.DEBUG);

// All we're really doing is setting up the data, calling the routine, and checking the results.
TestCSHOrgModelQuarterly x = new TestCSHOrgModelQuarterly();

Date tod = Date.Today();
// Calculate the dates we might use for AM_Completion depending on record type.
Date AM_Date_10 = Date.newInstance(tod.addMonths(1).year(),tod.addMonths(1).month(),10);
Date AM_Date_15 = Date.newInstance(tod.addMonths(1).year(),tod.addMonths(1).month(),15);


//Integer nCSHatStart = [select count() from CSH_New__c where Name like :unique and CreatedDate = Today];
//system.debug('Number of CSH\'s before the run is: ' + nCSHatStart);

// Instantiate the class which runs the code.
test.startTest();
CSHOrgModelQuarterly CSHR = new CSHOrgModelQuarterly();

String xxx = CSHR.doReview(true);

test.stopTest();



}
}

And the error is referencing these two lines :
insert User1;

TestCSHOrgModelQuarterly x = new TestCSHOrgModelQuarterly();

Does this mean my test records are not being made or is that something else ? I really appreciate any help I can get.

Thank you.

 

And the error is referencing these two lines : 

 insert User1;

 

TestCSHOrgModelQuarterly x = new TestCSHOrgModelQuarterly();

 


Does this mean my test records are not being made or is that something else ?  I really appreciate any help I can get.

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
Abhi_TripathiAbhi_Tripathi

Hi,

 

there you are trying to insert a user, which is not good idea if you just want a user Id

If you just want to have a users id the go for this method "userInfo.getUserId()" this method will give you current users Id, At this point no need to insert a user in test class.

 

And about that error , you have not provided a value that should be intered, you didn't provided the maine class, So not so confident about this

 

For test class basics refer this post

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

All Answers

Abhi_TripathiAbhi_Tripathi

Hi,

 

there you are trying to insert a user, which is not good idea if you just want a user Id

If you just want to have a users id the go for this method "userInfo.getUserId()" this method will give you current users Id, At this point no need to insert a user in test class.

 

And about that error , you have not provided a value that should be intered, you didn't provided the maine class, So not so confident about this

 

For test class basics refer this post

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

This was selected as the best answer
Zoom_VZoom_V

Abhi - Thank you very much for the advice on the userInfo.getUserId() method. I am a beginner and have never been shown that before. 

 

As it is right now, my code looks like this : 

 

@isTest
    private class TestCSHOrgModelQuarterly {
            static Id getRecordTypeId(String sObjectType, String Name) {
            return [select id from recordtype where sobjecttype=:sObjectType and name=:Name limit 1].id;
        }
            static final Id Org_Rectype = getRecordTypeId('Organization_Model__c','Global');
            static final Id A_MM_Rectype = getRecordTypeId('Account','Multi');
            
            Account Acc1;
            Organization_Model__c Org1;
           
            String u;  // Unique number for this test run.
            
            TestCSHOrgModelQuarterly() {
                
                // Create a unique string for this test so we're never confused with existing data.
                u = Datetime.now().millisecond().format();
                           
                Acc1 = new Account(Name=u+'Test1', recordtypeid=A_MM_Rectype, status_corporate_travel__c = 'Client', ownerid=UserInfo.getUserId(), billingcountry='USA');
                insert Acc1;  
                            
                Org1 = new Organization_Model__c(Name=u+'Test2', recordtypeid=Org_Rectype, Org_Model_CSH_to_be_completed_by__c = UserInfo.getUserId(), Account__c = Acc1.id, Org_Model_CSH__c = 'Quarterly');
                insert Org1;            
                }
        
            static testMethod void testReview() {
            // Reduce the number of message we get during debugging.
            system.debug(Logginglevel.DEBUG);
            
            // All we're really doing is setting up the data, calling the routine, and checking the results.
            TestCSHOrgModelQuarterly x = new TestCSHOrgModelQuarterly();
            
            Date tod = Date.Today();
            // Calculate the dates we might use for AM_Completion depending on record type.
            Date AM_Date_10 = Date.newInstance(tod.addMonths(1).year(),tod.addMonths(1).month(),10);
            Date AM_Date_15 = Date.newInstance(tod.addMonths(1).year(),tod.addMonths(1).month(),15);        
          
            test.startTest();
            CSHOrgModelQuarterly CSHR = new CSHOrgModelQuarterly();
           
            String xxx = CSHR.doReview(true);
                    
            test.stopTest();
            CSH_New__c Org1Csh = [select id, Due_Date__c from CSH_New__c where Organization_Model__c = x.Org1.id ];
        system.assertEquals(AM_Date_10, Org1Csh.Due_Date__c);
             }
    }

 I am getting this error when attempting to save it : 

 

unexpected token: 'x.Org1.id' 

 Do you have any idea why I'm getting that ? I can't see what's wrong here.

Thank you very much for any help you can give.

 

 

Abhi_TripathiAbhi_Tripathi

You cannot compare a field to value to an instance of a class, and you are initiallizing that class in your method, give me your email id i'll send you a test class for reference

 

//This will initiallize the contructor that will return all variable to null
TestCSHOrgModelQuarterly x = new TestCSHOrgModelQuarterly();