You need to sign in to do that
Don't have an account?
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.
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
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
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 :
I am getting this error when attempting to save it :
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.
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