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
rksfdcrksfdc 

TestMethods, Eclipse, & SFDC UI (Urgent)

Hi,
I have created a testmethod that simply creates a new opportunity, my issue is if I run the testmethod through Eclipse I am getting System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, java.lang.NullPointerException. But if I run the same testmethod through SFDC UI I don't get this error message, has anyone faced this issue before? Any help is really appreciated, I have inserted the source code for your reference.

Code:
@isTest
private class InitOppLineItemTest {

     static testMethod void myUnitTest() {
  Account existingAccount = [select Id, name from Account limit 1];
  RecordType existingRecordType = [Select r.SobjectType, r.NamespacePrefix, r.Name, r.IsActive, r.Id, r.DeveloperName, r.Description From RecordType r where SobjectType = 'Opportunity' limit 1];
  User user = [Select u.Id From User u where u.IsActive = true limit 1];
  
        Opportunity opp = new Opportunity();
     opp.recordTypeId = existingRecordType.id;
     opp.name=existingAccount.name + ': ' + 'TestOpp';
     opp.end_date__c = Date.today().addDays(31); 
     opp.CONSOLE_2_Schedule_Start_Date__c = Date.today();
     opp.closeDate = Date.today().addDays(31);
     opp.Amount_Digital__c = 300000;
     opp.Business_Unit__c = 'ALL01';
     opp.stageName = 'Engaged';
     opp.AccountId = existingAccount.id; 
     opp.OwnerId = user.id; 
     opp.CurrencyISOCode = 'USD';
                
        insert opp;
        // System.assert(false);
    }
}

 
Thanks again,
RK
DManelskiDManelski
Shot in the dark, I'm relatively new to Apex code myself. Is it possible that the user your SOQLing doesn't have the permissions to own an opportunity?  Try commenting out the OwnerId line. I'm sure an expert could spot your error right away but when you need to troubleshoot these types of problems yourself, try adding debug statement to your code.  See which Account you've grabbed from your SOQL, see which record type and user.
JimRaeJimRae
I would agree, it could be the user that you are setting as the owner doesn't have permissions to create an opportunity at all, or doesn't have permission to create one under that account. 
A better practice would be to create a new user, new account and new opportunity, then you are sure it will all work.  Just make sure the user you create has a profile and role consistent with your use case for the code.  Remember, these objects don't actually get stored in the database, they are only there for the test.

Code:
@isTest
private class InitOppLineItemTest {

    static testMethod void myUnitTest() {
     String profileid = [select id from profile where name='sales user' LIMIT 1].id;//set to proper name
        String roleid = [select id from userrole where name='sales role' LIMIT 1].id;//set to proper name
        User user = new User(name='test user',profileid=profileid,userroleid=roleid);//add any other required fields
  try{
   insert user;
  }catch(DMLException e){
   system.assert(false,'Error Creating User:'+e.getDMLMessage(0));
  }       
       
        Account testAccount = new Account(name='Test Account',ownerid=user.id);
        try{
         insert testAccount;
  }catch(DMLException e1){
   system.assert(false,'Error Creating Account:'+e1.getDMLMessage(0));
  }
    
    RecordType existingRecordType = [Select r.SobjectType, r.NamespacePrefix, r.Name, r.IsActive, r.Id, r.DeveloperName, r.Description From RecordType r where SobjectType = 'Opportunity' limit 1];

  
        Opportunity opp = new Opportunity();
      opp.recordTypeId = existingRecordType.id;
      opp.name=testAccount.name + ': ' + 'TestOpp';
      opp.end_date__c = Date.today().addDays(31); 
      opp.CONSOLE_2_Schedule_Start_Date__c = Date.today();
      opp.closeDate = Date.today().addDays(31);
      opp.Amount_Digital__c = 300000;
      opp.Business_Unit__c = 'ALL01';
      opp.stageName = 'Engaged';
      opp.AccountId = testAccount.id; 
      opp.OwnerId = user.id; 
      opp.CurrencyISOCode = 'USD';
         
     try{        
         insert opp;
         system.assert(true,'Created Test Opportunity');
     }catch(DMLException e2){
      System.assert(false,'Error Creating Opp:'+e2.getDMLMessage(0));
     }
    }
}

 

rksfdcrksfdc
First of all thanks for the reply, but I even picked an Account & the Account Owner and hard-coded their ID in the testmethod (which would not throw any permission issue), still it was failing. But you have to remember that it fails ONLY in Eclipse, if you run the same testmethod through Salesforce UI, it does not throw that error message. :(

~thanks
JimRaeJimRae
Are you logged into the same instance of SFDC and using the same user id and password?  A null pointer exception means that some field you are referencing is not getting populated. 
You could also try adding system.debug statements to see if something isn't returning properly.  Run this version with Eclipse, and if you don't see where the issue is, post the entire debug log.

Code:
private class InitOppLineItemTest {

     static testMethod void myUnitTest() {
  Account existingAccount = [select Id, name from Account limit 1];
system.debug('\n\nexistingAccount='+existingAccount);
  RecordType existingRecordType = [Select r.SobjectType, r.NamespacePrefix, r.Name, r.IsActive, r.Id, r.DeveloperName, r.Description From RecordType r where SobjectType = 'Opportunity' limit 1];
system.debug('\n\nexistingRecordType ='+existingRecordType);
  User user = [Select u.Id From User u where u.IsActive = true limit 1];

system.debug('\n\nSelect User ='+user);
  
        Opportunity opp = new Opportunity();
     opp.recordTypeId = existingRecordType.id;
     opp.name=existingAccount.name + ': ' + 'TestOpp';
     opp.end_date__c = Date.today().addDays(31); 
     opp.CONSOLE_2_Schedule_Start_Date__c = Date.today();
     opp.closeDate = Date.today().addDays(31);
     opp.Amount_Digital__c = 300000;
     opp.Business_Unit__c = 'ALL01';
     opp.stageName = 'Engaged';
     opp.AccountId = existingAccount.id; 
     opp.OwnerId = user.id; 
     opp.CurrencyISOCode = 'USD';
  
system.debug('\n\nopporutnity to insert ='+opp);
              
        insert opp;
        // System.assert(false);
    }
}

 

rksfdcrksfdc
Thanks JimRae, I ran the same code through Eclipse and SalesForce UI, here are the results. You can notice the DmlException in Eclipse but not in Salesforce UI

Eclipse
Code:
Debug Log:


Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090109152412.069
*** Ending Workflow Evaluation20090109152411.692:Class.InitOppLineItemTest.myUnitTest: line 53, column 9:     DML Operation executed in 326 ms
System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, java.lang.NullPointerException

Class.InitOppLineItemTest.myUnitTest: line 53, column 9


Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 3 out of 100
Number of query rows: 3 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 100
Number of DML rows: 1 out of 500
Number of script statements: 20 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

Testing resources:

Total email recipients queued to be sent : 0
Stack frame variables and sizes:
  Frame0

*** Ending Test InitOppLineItemTest.public static testMethod void myUnitTest()

Cumulative profiling information:

No profiling information for SOQL operations.

No profiling information for SOSL operations.

No profiling information for DML operations.

No profiling information for method invocations.

 Salesforce UI

Code:
Template: 00X30000000tqS9
Recipients: fred_kao@timeinc.com william_lee@timeinc.com
CcEmails: PS-SFDC_integration@timeinc.com
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090109152433.496
*** Ending Workflow Evaluation20090109152432.889:Class.InitOppLineItemTest.myUnitTest: line 53, column 9: DML Operation executed in 594 ms
20090109152432.889:Class.InitOppLineItemTest: line 30, column 40: returning from end of method public static testMethod void myUnitTest() in 608 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 3 out of 100
Number of query rows: 3 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 100
Number of DML rows: 1 out of 500
Number of script statements: 20 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

Testing resources:

Resource usage for namespace: (default)
Number of SOQL queries: 2 out of 20
Number of query rows: 0 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 100
Number of script statements: 19 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 1 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0
*** Ending Test InitOppLineItemTest.public static testMethod void myUnitTest()

 


JimRaeJimRae
Did you add the debug lines to your testmethod? It is odd that none of them printed out in your debug log.
rksfdcrksfdc
Yes I did, I noticed it's weird.
rksfdcrksfdc
The funny thing is even if I run the below testmethod I get the same error message (only thru Eclipse mind you) as above. Anyone faced this please let me know.

Code:
    public static testMethod void createAccount()
    {
     Account newAcc = new Account();
     newAcc.name = 'TestAcc';
     newAcc.currencyisocode = 'USD';
     insert newAcc; 
    }

 

DManelskiDManelski
Are you sure that you're creating a new account with all of the required fields populated?