1

I am trying to cover my class Api_Http_DealerProfileEmail_1 using a test class which covers some other classes too,for some reason it keeps on giving me 0% code coverage for the class. Here is the part of test class, I am using to cover my class. Can anyone please tell me whats wrong here. Thanks.

  private static void insertAndChangeAddress() {
    Integer r = Crypto.getRandomInteger();
    String rs = String.valueOf(r);
    String email = 'jb-'+rs+'@some.org';
    String newEmail = 'jb-'+rs+'@other.org';
    Id id;
    Id testId;

    User owner = [select Id, Name from User where Name = 'OEM Sales' limit 1];

    id = Api_Http_DealerRegister_0.postNewCustomer('JoeBloe','GM','12345',email,'1212 some place'
          ,'Bouler','CO','12121','US','3033539876','Joe','Bloe');
    System.assertNotEquals(null, id);

Account acct = [select Id, OwnerId,  Dealer_Email__c  from Account where Id = :id limit 1];
System.assertEquals(owner.Id, acct.OwnerId);
System.assertEquals(email, acct.Dealer_Email__c);

// Change the email address to a duplicate email address, and fail
Api_Result_0 result = Api_Http_DealerProfileEmail_1.setEmail(user1Email, newEmail);
System.assertEquals(false, result.success);

// Change the email address correctly now
result = Api_Http_DealerProfileEmail_1.setEmail(email, newEmail);
System.assertEquals(true, result.success);

// Make sure we can look up the account by the modified address
acct = [select Id, OwnerId,  Dealer_Email__c  from Account where  Dealer_Email__c  = :newEmail limit 1];
System.assertEquals(id, acct.Id);

// The same rename operation tried again should fail, because the original email no longer exists
result = Api_Http_DealerProfileEmail_1.setEmail(email, newEmail);
System.assertEquals(false, result.success);
}

private static void updateNoContactRecord() {
  String email = '[email protected]';
    RecordType customerType = [Select Id, Name, sObjectType 
        from RecordType
        where sObjectType = 'Account' and Name='Company OEM'
        limit 1
    ];

    Account acct = new Account();

    acct.RecordTypeId = customerType.Id;      
acct.Name = 'Joe Shmoe';
acct.ShippingStreet = '2234 Mercury Way';
acct.ShippingCity = 'Santa Rosa';
acct.ShippingState = 'CA';
acct.ShippingCountry = 'US';
acct.ShippingPostalCode = '94928';

    acct.Dealer_Email__c = email;
    acct.Type = 'Customer';

    insert acct;

String encEmail = EncodingUtil.urlEncode(email, 'utf-8');
System.RestContext.request = new RestRequest();
RestContext.request.requestURI = '/Api/DealerProfile_0/' + encEmail;

Api_Result_0 result = Api_Http_DealerProfile_0.setProfile(
  'JaneShmoe',
  '1121 Some Other Place','Denver', 'CO', '12345', 'US', 
  '7075551212', 'Jane','Shmoe'
);

    Contact cont = [select Id, Email, Name, FirstName, LastName, MailingStreet, MailingCity, MailingState, 
      MailingCountry, MailingPostalCode from Contact where email = :email limit 1];
    System.assertNotEquals(null, cont);
    System.assertEquals(email, cont.Email);
    System.assertEquals('Shmoe', cont.LastName);
}

private static void readdressNoContactRecord() {
  String email = '[email protected]';
  String newEmail = '[email protected]';
  Contact cont;

    RecordType customerType = [Select Id, Name, sObjectType 
        from RecordType
        where sObjectType = 'Account' and Name='Subscriber(new)'
        limit 1
    ];

    Account acct = new Account();

    acct.RecordTypeId = customerType.Id;      
acct.Name = 'Pliny A Younger';
acct.ShippingStreet = '2234 Mercury Way';
acct.ShippingCity = 'Santa Rosa';
acct.ShippingState = 'CA';
acct.ShippingCountry = 'US';
acct.ShippingPostalCode = '94928';

    acct.Dealer_Email__c = email;
    acct.Type = 'Customer';

    insert acct;

Api_Result_0 result = Api_Http_DealerProfileEmail_1.setEmail(email, newEmail);
    System.assertEquals(true, result.success);

    // make sure we can't retrieve the original contact
    try {
      cont = [select Id from Contact where email = :email limit 1];
      System.assert(false, 'We should not be able to retrieve the contact by the old email.');
    } catch (QueryException qex) {

    }

    cont = [select Id, Email, Name, FirstName, LastName, MailingStreet, MailingCity, MailingState, 
      MailingCountry, MailingPostalCode from Contact where email = :newEmail limit 1];
    System.assertNotEquals(null, cont);
    System.assertEquals(newEmail, cont.Email);
    System.assertEquals('Younger', cont.LastName);
    System.assertEquals(acct.ShippingStreet, cont.MailingStreet);
    System.assertEquals(acct.ShippingPostalCode, cont.MailingPostalCode);

}

And my class is :

   @RestResource(urlMapping='/Api/DealerProfileEmail_1/*')
   global with sharing class Api_Http_DealerProfileEmail_1 {

  @HttpPost
  global static Api_Result_0 setEmail(
    String oldEmail,
    String newEmail
  ) {
    Api_Result_0 result = new Api_Result_0();

    try {
      Api_DealerProfile_0 profile = new Api_DealerProfile_0(oldEmail);

      profile.changeEmail(newEmail);
      result.success = true;
    } catch(Exception ex) {
      result.success = false;
      result.error = ex.getMessage();
    }
    return result;
  }
}
2
  • It looks like everything should be covered. Perhaps you're just running into a code coverage bug? Consider running the test in Force.com IDE or another tool and see if you get a different result.
    – sfdcfox
    Commented May 13, 2016 at 16:25
  • I tried that too @sfdcfox, still no coverage.I tried create even a separate test class which again didn't help.I dont know what else to try now. Commented May 13, 2016 at 18:13

1 Answer 1

0

Test methods must be marked with @isTest or testMethod. Therefore, your tests aren't actually running. Just add @isTest before the function declarations.

@isTest static void insertAndChangeAddress() { ...

@isTest static void updateNoContactRecord() { ...

@isTest static void readdressNoContactRecord() { ...
2
  • It worked, @sfdc. But now when I try to upload the class to my production,i get error "All components failed Version Compatibility Check. Every component in this change set requires the "37.0" or higher platform version. Please select an organization with a platform version of "37.0" or higher." IS this related or something different. Commented May 13, 2016 at 18:53
  • Set your classes to v36. Your sandbox is in summer 16 but your production is only spring 16.
    – sfdcfox
    Commented May 13, 2016 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .