2012-08-28 25 views
0

我正在嘗試編寫一個測試方法,該方法要求我生成機會以及與該機會相關的所有記錄。我一直在最奇怪的地方收到空指針異常。具有空指針異常的Salesforce Apex測試類

public static void createOpp() { 
    OpportunityEscalationtest.a = new Account(Name = 'SGC Test Account' 
              , Type = 'Customer' 
              , Phone = '(00) 0000 0000'); 

    insert OpportunityEscalationtest.a; 

    OpportunityEscalationtest.c = new List<Contact>();  
    Contact newC = new Contact(FirstName = 'Jack' 
           , LastName = 'O\'Neil' 
           , Phone = '(00) 0000 0000' 
           , AccountId = OpportunityEscalationtest.a.Id); 

    OpportunityEscalationtest.c.add(newC); 

    newC = new Contact(FirstName = 'Samantha' 
         , LastName = 'Carter' 
         , Phone = '(00) 0000 0000' 
         , AccountId = OpportunityEscalationtest.a.Id); 

    OpportunityEscalationtest.c.add(newC); 

    newC = new Contact(FirstName = 'Daniel' 
         , LastName = 'Jackson' 
         , Phone = '(00) 0000 0000' 
         , AccountId = OpportunityEscalationtest.a.Id); 

    OpportunityEscalationtest.c.add(newC); 

    insert OpportunityEscalationtest.c; 

    Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1]; 
    OpportunityEscalationtest.a.GillForce__Primary_Contact__c = priCont.Id; 

    OpportunityEscalationtest.o = new Opportunity(Name = 'Mountain Complex Water' 
               , CloseDate = system.today() 
               , StageName = 'Business Analysis' 
               , AccountId = OpportunityEscalationtest.a.Id); 

    insert OpportunityEscalationtest.o; 

    for (Contact cont : c) { 
     OpportunityContactRole role = new OpportunityContactRole(ContactId = cont.Id 
                   , OpportunityId = OpportunityEscalationtest.o.Id 
                   , Role = 'Descision Maker'); 

     role.IsPrimary = (OpportunityEscalationtest.a.GillForce__Primary_Contact__c == cont.Id); 

     insert role; 
    } 
} 

錯誤是這2行代碼之間的某處拋出:

insert OpportunityEscalationtest.c; 

    Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1]; 

我有點爲難,除非是我弄錯了這段代碼應該是自包含的。任何想法都會非常出色。

+0

你是否運行這個代碼作爲測試方法,'testMethod public static void createOpp()'? –

回答

0

首先,我會嘗試將您的測試類的API版本設置爲22.0(您的類體的版本Sttings選項卡)並嘗試再次嘗試測試。

+0

爲什麼是22? 25可用。 –

+0

請看http://salesforceblogger.blogspot.in/2012/02/sfdc-api-240-test-failure-issue.html – mast0r

+0

因此,一個好的答案是解釋您在博客中閱讀的主要觀點並參考博客。然後人們看不到作爲解決方案提供的神奇數字。 –

0

你在網上

Contact priCont = [Select Id from Contact where FirstName = 'Jack' limit 1]; 

得到錯誤這是接縫插入失敗(可能是由於觸發某些情況下)。或者它可能不會插入一個名稱爲Jack的特定聯繫人(可能是因爲姓氏)。

如果我是你,我會怎麼做。我將檢索所有聯繫人並將其打印到調試(或者對您來說更容易)。在此之後,您將瞭解問題出在哪裏。