-1

I wrote a rest class and it works as follows. Rest class check for the lead. If lead is exist, converting it into account and contact and creating a portal user by using contact id. Rest class also check for exisitng usenames in User and return valid message like username already used if it found exsiting username. The code is given below.

@RestResource(urlmapping='/leadconvert/*')
Global class Leadconvert {
     @HttpPost
     global static string post(string firstname,string lastname,string email,string company) {
         List<Lead> lead= [select firstname,lastname,email,company,isconverted from Lead where Firstname=:firstname AND Lastname=:lastname And Email =: email And isconverted=false];
         List<User> use = [select id from User where Username =: email];
         try {
             if(use.size()>0){
                 return 'Given email is already used. please provide another email';
             }
             if(lead.size()>0 && use.size()==0){
                Database.LeadConvert lc = new database.LeadConvert();
                lc.setLeadId(lead[0].id);
                LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
                lc.setConvertedStatus(convertStatus.MasterLabel);
                Database.LeadConvertResult lcr = Database.convertLead(lc);

                Account a=[select id,name,phone from Account where id=:lcr.getAccountId()];
                Contact cc=[select id,firstname,Lastname,email from Contact where id=:lcr.getContactId()];
                ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;

                User u=new User();
                u.email=cc.email;
                u.contactid = lcr.getContactId();
                u.profileid = profileID; 
                u.UserName=email; 
                u.Alias=cc.firstname; 
                u.CommunityNickName=lastname; 
                u.TimeZoneSidKey='America/New_York';
                u.LocaleSidKey='en_US';
                u.EmailEncodingKey='ISO-8859-1'; 
                u.LanguageLocaleKey='en_US';
                u.FirstName = cc.firstname;
                u.LastName = cc.Lastname;  
                insert u;    

                return u.id;
             }else {
                 return 'Lead is not exit, please give valid values';
             }
         }catch(exception e){
             return e.getMessage();
         }
    }
}

The above code is working fine. But I wrote a test class for the above rest class and its giving StringException, it does not covering full class as well.

@isTest
Private class Test_Leadconvert {

    Static TestMethod void  TestPostMethod1() {
        Account acc = new Account(Name = 'Exp');
        insert acc;
        Contact con = new Contact(Lastname = 'Lst Name', AccountId=acc.Id);
        insert con;
        ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;

        User u=new User();
        u.email= 'variation1@gmail.com';
        u.contactid = con.Id;
        u.profileid = profileID; 
        u.UserName= 'dummy@gmail.com'; 
        u.Alias= 'KL'; 
        u.CommunityNickName= 'perk55'; 
        u.FirstName = 'Test Name';
        u.LastName = 'KL';
        u.TimeZoneSidKey= 'America/New_York';
        u.LocaleSidKey= 'en_US';
        u.EmailEncodingKey= 'ISO-8859-1'; 
        u.LanguageLocaleKey= 'en_US';
        insert u;
        System.runAs(u) {
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/leadconvert ';
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response= res;
        String name = Leadconvert.post('Name1','lname','dummy@gmail.com','bizs');
        System.assertEquals('Given email is already used. please provide another email',name);
        }
    }

    Static TestMethod void  TestPostMethod2(){

        Lead leed = new Lead(Firstname='Name2',Lastname='Example2',Email='myorg2@gmail.com',Company='My Org2',Status='Open - Not Contacted');
        insert leed;
        database.leadConvert lc = new database.leadConvert();
        lc.setLeadId(leed.id);
        leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);

        Account a = [select id,name,phone from Account where id=:lcr.getAccountId()];
        Contact con = [select id,firstname,Lastname,email from Contact where id=:lcr.getContactId()];

        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
        ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;
        Id userId;
        System.runAs (thisUser) {
            User u = new User();
            u.email= 'variation1@gmail.com';
            u.contactid = con.Id;
            u.profileid = profileID; 
            u.UserName= 'myorg2@gmail.com'; 
            u.Alias= 'KL'; 
            u.CommunityNickName= 'perk55'; 
            u.FirstName = 'Test Name';
            u.LastName = 'KL';
            u.TimeZoneSidKey= 'America/New_York';
            u.LocaleSidKey= 'en_US';
            u.EmailEncodingKey= 'ISO-8859-1'; 
            u.LanguageLocaleKey= 'en_US';
            insert u;
            userId = u.id;
        }
            RestRequest req = new RestRequest(); 
            RestResponse res = new RestResponse();
            req.requestURI = '/services/apexrest/leadconvert ';
            req.httpMethod = 'POST';
            RestContext.request = req;
            RestContext.response= res;
            String name = Leadconvert.post('Name2','Example2','dummy2@gmail.com','My Org2');
            System.assertEquals(userId,name); //error is coming here
     }
}

I could not able to sole this issue and please help me to cover the total class also.

3
  • What is the exact error and on what line is it happening. Not line number the actual line of code
    – Eric
    Apr 17 '17 at 7:25
  • Getting StringException while checking assertion in method: TestPostMethod2. I already mention in test class where i am facing issue. Apr 17 '17 at 7:32
  • Please include the error message verbatim or this exercise just devolves into a guessing game.
    – Adrian Larson
    Apr 17 '17 at 10:51
1

Other other than your if(lead.size()>0 && use.size()==0) statement all return statement will pass longer text message. so in your textmethod you do System.assertEquals(id,string), When you perform a comparison between a String and an Id (e.g. ==, !=, or <>), then the other argument is "promoted" to an Id value. All Id values must be 15 or 18 characters long, and must contain only A-Z, a-z, and 0-9, so '' is not a valid Id. refere this answer

and to cover your if(lead.size()>0 && use.size()==0) remove the lead conversion code in your test method. so that you will get the lead when you query in your controller and convert in your controller which is already written.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.