You need to sign in to do that
Don't have an account?
vickim1.396005552708281E12
Unit testing coverage issue!
Hi Guys,
I have created 1 trigger and one class in sandbox, I have written 2 test classes for this, one test for the trigger and one test for the class.
When I run the 'test run' in Sandbox it say Class have 100% coverage, however when trying to deploy the code in prod I get errors saying coverage is only 50%
Code Below
Trigger :
My class
TEST CLASSES
testSync()
test for my trigger
I have created 1 trigger and one class in sandbox, I have written 2 test classes for this, one test for the trigger and one test for the class.
When I run the 'test run' in Sandbox it say Class have 100% coverage, however when trying to deploy the code in prod I get errors saying coverage is only 50%
Code Below
Trigger :
trigger myTrigger on Opportunity (after update) { string prams; // partner portal auth details prams = 'userkey=salesforce'; prams = prams + '&passkey=password'; // action occured in saledforce prams = prams + '&action=OpportunityUpdated'; for (Opportunity op: trigger.new) { Opportunity oldOpportunity = Trigger.oldMap.get(op.Id); prams = prams + '&id[]=' + op.Id; // Opportunity ID prams = prams + '&name[]=' + op.Name; prams = prams + '&oldName[]=' + oldOpportunity.Name; // previous Name prams = prams + '&stageName[]=' + op.StageName; prams = prams + '&oldStageName[]=' + oldOpportunity.StageName; // previous stage prams = prams + '&closeDate[]=' + op.CloseDate; prams = prams + '&oldCloseDate[]=' + oldOpportunity.CloseDate; // previous close date prams = prams + '&closeLostReason[]=' + op.Closed_Lost_Reason__c; prams = prams + '&ownerId[]=' + op.OwnerId; } Sync.invokeUrlWithPost('http://myurl.com', prams); }
My class
public class Sync{ //Future annotation to mark the method as async. @Future(callout=true) public static void invokeUrlWithPost(string url, string prams){ if(url.length() > 0 && prams.length() > 0){ HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setEndpoint(url); req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); req.setBody(prams); Http http = new Http(); try { HttpResponse res = http.send(req); system.debug('B:STATUS:'+res.getStatus()); } catch(system.Exception e) { system.debug('B:Callout error:'+e); } } } }
TEST CLASSES
testSync()
@isTest public class testSync{ static testMethod void testInvokeUrlWithPost(){ String url = 'http://myurl.com'; String prams; // partner portal auth details prams = 'userkey=salesforce'; prams = prams + '&passkey=password'; // action occured in saledforce prams = prams + '&action=testInvokeUrlWithPost&Name=test'; test.startTest(); System.assertEquals(url, 'http://myurl.com'); Sync.invokeUrlWithPost(url, prams); test.stopTest(); } static testMethod void testInvokeUrlWithPost_noPrams(){ String url = 'http://myurl.com'; String prams = ''; test.startTest(); Sync.invokeUrlWithPost(url, prams); test.stopTest(); } static testMethod void testInvokeUrlWithPost_invalidUrl(){ String url = ''; String prams; // partner portal auth details prams = 'userkey=salesforce'; prams = prams + '&passkey=password'; // action occured in saledforce prams = prams + '&action=testInvokeUrlWithPost&Name=test'; test.startTest(); Sync.invokeUrlWithPost(url, prams); test.stopTest(); } }
test for my trigger
@isTest public class testMyTrigger{ static testMethod void testPurplePortalUpdate(){ // create a new Opportunity Opportunity testOpp = new Opportunity( Name = 'My Test Opp', StageName = 'Qualification', CloseDate = System.today().addDays(4)); // insert it to the db insert testOpp; // get the inserted Opportunity to edit it Opportunity oldOpp = [SELECT Name, StageName, CloseDate FROM Opportunity WHERE id = :testOpp.Id]; // error if the Opportunity did not add System.assert(testOpp.Id != null, 'The Test opportunities did not insert properly, please check validation rules and other mechanisms'); // change its name oldOpp.Name = 'Updated'; System.assertEquals(oldOpp.Name, 'Updated'); System.assertEquals(oldOpp.StageName, 'Qualification'); // Start the test, this changes governor limit context to // that of trigger rather than test. test.startTest(); // update the Opportunity update oldOpp; // Stop the test, this changes limit context back to test from trigger test.stopTest(); } }
All Answers
you can also do it through the ui.
Setup > Develop > Classes > Run All Tests
Then go to the same page and click to view code coverage
"Code Coverage: 0%
Your overall code coverage is currently 0%. To deploy code to production, you must have at least 75%."
How would I fix this as I have not coded or added any manuall code in the prod myself?
open your developer console in production environment - click test - clear test data
then click test - run all
in the bottom window you should see on the test tag the test kick off - once it's finished check the overall code coverage window on the right and take a look at what code coverage is.
then go back to the UI and check the code coverage for the org again.
hopefully after you've done this it won't be 0% anymore.
if it is you'll need some help to get your code coverage back up to a decent level.
Is there a way I can bypass this, as I need my new code to get to prod
it's only 27 lines of code so it might be an easy one to fix.
if you post a copy of the code i can take a look for you..it might just need a couple of records creating..
One issue I did find is, this code/class (AutocreatedRegHandler1391697059641) does not exist in my sandbox env, I am guessing I will just need to create this class in the sandbox?
Cheers,
that code is autogenerated..
too
to remove problems, the because the class is auto generated, it wont changes names etc will it?
cheers
dan
I have managed to deploy my changes, my new issue is, although everything is deployed the changes are not taking effect in prod, so for example the invoke method I had in sandbox was calling my url and I could track this on my server, however in prod when I make changes to oportunities, my url is not invoked, seems like my deployed changes have not taken affect? anything I need to lookout for?
Thank you
i have not made any code changes in salesforce sandbox or prod code, migrated the 100% same code
Setup > Security Controls > Remote Sites
Make sure your URL in added in there - if I remember rightly in a sandbox you don't need to add the URL.
thanks
dan
I am guessing I will need to wait for few hours for my changes to take affect? its been 3 hours since my changes went live
the other thing to do is enable debugging on your account and then try and see what's going on with the apex - if there is a callout problem or any kind of problem it will be flagged in there.
my concern is considering you had code in production that wasn't in your sandbox there might be something else that is in production(required field) that wasn't in your sandbox and so hopefully the debugging will pick that up.
cheers
dan
then new
Find your account and go through the process that you believe should fire the trigger - as it happens you'll see some logs appears in debug logs windows.
thanks
dan