2017-12-12 3 views
0

에 대한 테스트 클래스 저는 Apex Development에서 새로 왔습니다. 내 Apex 트리거에 대한 TestClass를 작성하고 싶습니다.Apex의 테스트 클래스

당신과 함께 내 코드를 공유하고있다 :

trigger ClosedOpportunityTrigger on Opportunity (before update) { 
    for(Opportunity o:Trigger.New) { 
     if(o.Probability==100 && o.Invoiced__c == true) 
     {    
      Attachment a = new Attachment(); 
      try { 
       a = [Select Id, Name from Attachment where ParentId =:o.Id]; 
      } 
      catch(Exception e) { 
       a = null; 
      } 
      if (a == null) 
       o.addError('Add an attachment before you close the Opportunity'); 
      } 
     } 
    } 

답변

1

goodluck는

public static void testmethod(){ 

opportunity opp = new opportunity() 
//change whatever fields u need to make probability 100% 
opp.stage = 'Closed Won'; 
opp.Invoiced__c == true; 

try{ 

insert opp 
} 
catch(Exception e){ 

string errormessage = e.getMessage(); 

} 
//now that u know how to do it, do a positive test where you also add an   
//attachment 
//as a side note, your catch block will never fire, because you aren't  
//catching any exceptions you are just getting a null string 
} 
+0

안녕하세요, Tim, 오류가 발생했습니다 : "오류 : 컴파일 오류 : 예기치 않은 토큰 'void'. 행 2 열 15 \t" –

+0

감사합니다. 정말 많이 변경되었습니다. 괜찮습니다. –

+0

논리가있는 의사 코드가 더 많았습니다. 그것의 뒤에, 나는 또한 당신이 당신의 방아쇠에있는 당신의 catch 블록이 잘못되었다고 말하면서 마지막에 나의 노트를 보았기를 바랍니다. –

2

가 수행해야 2 가지가 있습니다 : - 1. '기회'개체의 레코드를 작성합니다. 코드를 사용하여 업데이트하십시오. 2. 첨부 파일을 만듭니다. 여기 https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ

+0

트리거가 잘 작동하지만, II 위의 트리거에 대한 코드를 작성하는 방법을 모르는 참조에 대한 링크입니다. –