Apex测试方法错误

时间:2012-05-04 07:07:22

标签: testing salesforce apex-code

我们遇到了顶点测试方法的问题。我的apex方法中有一个http请求。我必须为此创建一个测试方法。但我们遇到了问题

Apex代码:

public void ReceiveBestAnswer(Id repID){    
        reply = GlobalFunctions.getReplyCreatorDetails(repID);
        System.debug(reply);
        String bestReplyUser = reply.CreatedById;


        profileUser = [select Username,Email from User where Id = :bestReplyUser];
        System.debug('profileUser.Name-->'+profileUser.Username);  
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

        req.setEndpoint('http://sandbox.v2.badgeville.com/api/berlin/b95839370dca983955e550296450ec03/activities.json');
        req.setMethod('GET');
        req.setBody('activity[verb]=receive best answer&site=www.grazitti.com&user='+ profileUser.Email );
        try {
            http.send(req);
            System.debug('res-->');
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        } 
}  

测试方法:

 public static testMethod void testReceiveBestAnswer() {        
 Reply reply = new Reply();
    String replyId = reply[0].Id;
  //  Id questionID = '906P0000000010KIAQ';
  //  String sql =  'SELECT Id, Title FROM Question';  
 //   Question[] ques = [SELECT Id FROM Question];      
 //   Question quest = ques[0];       
    BadgeVilleWebServiceCallout bv = new BadgeVilleWebServiceCallout();
    bv.badgvilleReceiveBestAnswer(replyId );

}

第二种测试方法:

 public static testmethod  void testReceiveBestAnswer1() 
     {

        ID ProfileID = [ Select id from Profile where name = 'Named Marketo Community Portal'].id;
        System.debug('ProfileID --->'+ProfileID );
        Account A1 = new Account(Name = 'Test Account');
        insert A1;        
        List<Contact> ContactList = new List<Contact>();
        Contact C1 = new Contact( AccountID = A1.id, FirstName = 'Test', LastName = 'User', 
            email = 'test-user@fakeemail.com' );
        insert C1;


        User u1 = new User( email='test-user@fakeemail.com', contactid = c1.id, profileid = profileid, 
            UserName='test-user@fakeemail.com', alias='tuser1', CommunityNickName='tuser1', TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1',  LanguageLocaleKey='en_US', FirstName = 'Test', LastName = 'User' );
    insert u1;
    System.debug('u1--->'+u1 );




      /* User[] users = [SELECT Id FROM User];
      User u1 = users[0];

      //  Question[] ques = [SELECT Id FROM Question];  
       //System.debug('ques --->'+ques );    
 //   Question quest = ques[0];       
 //   BadgeVilleWebServiceCallout bv = new BadgeVilleWebServiceCallout();
 //   bv.badgvilleReceiveBestAnswer(quest.Id);

 Id questId ;
   Test.startTest();  
   System.RunAs(u1)
        {            
            Question question = new Question();
            question.Title = 'Test';
            question.Body = 'Test';
            question.CommunityId = '09a50000000PNNr';//GlobalFunctions.communityId;
            insert question;  
            System.debug('question-->'+question);              
            questId = question.Id;   
            System.debug('questId --->'+questId );  

            Reply reply1 = new Reply();                
            reply1.Body =  'reply no 1 ques 1';
            reply1.QuestionId = question.Id;                 
            System.debug('reply1--->'+reply1);        
            insert reply1;
        }


      //  BadgeVilleWebServiceCallout bv = new BadgeVilleWebServiceCallout();
     //   bv.badgvilleReceiveBestAnswer(questId);   
        Test.stopTest();    


    }

我们正试图用两种测试方法来实现它。但我们无法做到。能不知道怎么做这个或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

Salesforce不允许您测试外部Web服务。

在这里,您了解如何test WebServices

这里有一个Salesforce Idea

我希望这对你有所帮助。