I am sorry if this has been answered here before, I was not able to find an answer.
I made a small modification[changed an if condition, no new lines added or removed] to one of the apex classes in the sandbox. I created a changeset to deploy that to production, for some reason I am getting the code coverage failure error message. I tried deploying it with run specified test as well, still didn't help. Below is the snip of the error message.
I need this to be pushed to production as soon as possible. Any idea how to make this work?
Edited - Here is the if condition I modified and I am including the pre-written test class here as well, please advise!
**APEX CONTROLLER CLASS**
@AuraEnabled
public static List<SimpleAccountBudget> getAccountBudgets(Id accountId) {
List<SimpleAccountBudget> accountBudgets = new List<SimpleAccountBudget>();
Date today = Date.today();
List<String> years = new List<String>();
//if (today.month() == 1 || today.month() == 2) { backup for budget change
if (today.month() == 1) {//changed to this IF from the above line
years.add(String.valueof(today.year() - 1));
}else{
years.add(String.valueof(today.year()));
}
AggregateResult[] budgetTotals = [SELECT Account__c, Year__c, SUM(Amount__c) Amount FROM Coop_Budget__c WHERE Account__c = :accountId AND Year__c IN :years GROUP BY Account__c, Year__c];
AggregateResult[] paymentTotals = [SELECT Account__c, Coop_Budget_Year__c, SUM(Amount_Payable__c) Amount FROM Payment_Request__c WHERE Account__c = :accountId AND Status__c = 'Approved' AND Coop_Budget_Year__c IN :years GROUP BY Account__c, Coop_Budget_Year__c];
Map<String, SimpleAccountBudget> calculatedAccountBudgets = calculateAccountBudgets(budgetTotals, paymentTotals);
for (String year : years) {
String budgetKey = (String)accountId + '-' + year;
if (calculatedAccountBudgets.containsKey(budgetKey)) {
accountBudgets.add(calculatedAccountBudgets.get(budgetKey));
} else {
SimpleAccountBudget accountBudget = new SimpleAccountBudget();
accountBudget.AccountId = accountId;
accountBudget.Year = year;
accountBudgets.add(accountBudget);
}
}
return accountBudgets;
}
TEST CLASS
@isTest
private class PaymentRequestControllerTests {
@isTest static void testGetPaymentRequests() {
User u = createUsersAndReturnOne();
System.runAs(u) {
Coop_Campaign__c coopCampaign = createCoopCampaign();
CoopCampaignController.submitForPaymentReview(coopCampaign.Id);
CoopCampaignController.approvePaymentRequests(coopCampaign.Id);
PaymentRequestController.SimplePaymentRequests simplePaymentRequests = PaymentRequestController.getPaymentRequests();
System.assert(simplePaymentRequests.PaymentRequests.size() > 0);
}
}
@isTest static void testGetPaymentRequest() {
User u = createUsersAndReturnOne();
System.runAs(u) {
Coop_Campaign__c coopCampaign = createCoopCampaign();
Payment_Request__c paymentRequest = [SELECT Id FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
PaymentRequestController.SimplePaymentRequest simplePaymentRequest = PaymentRequestController.getPaymentRequest(paymentRequest.Id);
System.assert(simplePaymentRequest.Id == paymentRequest.Id);
}
}
@isTest static void testGetPartialInvoices() {
User u = createUsersAndReturnOne();
System.runAs(u) {
Coop_Campaign__c coopCampaign = createCoopCampaign();
List<PaymentRequestController.SimpleInvoice> invoices = PaymentRequestController.getPartialInvoices(coopCampaign.Account__c);
System.assert(invoices.size() == 1);
}
}
@isTest static void testApprovePaymentRequest() {
User u = createUsersAndReturnOne();
System.runAs(u) {
Coop_Campaign__c coopCampaign = createCoopCampaign();
CoopCampaignController.submitForPaymentReview(coopCampaign.Id);
CoopCampaignController.approvePaymentRequests(coopCampaign.Id);
Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
PaymentRequestController.approvePaymentRequest(paymentRequest.Id);
paymentRequest = [SELECT Id, Status__c, Amount_Payable__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
System.assert(paymentRequest.Status__c == 'Approved');
List<PaymentRequestController.SimpleAccountBudget> accountBudgets = PaymentRequestController.getAccountBudgets(coopCampaign.Account__c);
PaymentRequestController.SimpleAccountBudget accountBudget = accountBudgets[0];
//System.assert(accountBudget.Year == String.valueOf(Date.today().year()));
}
}
@isTest static void testCancelPaymentRequest() {
User u = createUsersAndReturnOne();
System.runAs(u) {
Coop_Campaign__c coopCampaign = createCoopCampaign();
Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
PaymentRequestController.cancelPaymentRequest(paymentRequest.Id);
paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
Coop_Campaign__c updatedCoopCampaign = [SELECT Id, Payment_Status__c FROM Coop_Campaign__c WHERE Id = :coopCampaign.Id];
System.assert(paymentRequest.Status__c == 'Cancelled');
System.assert(updatedCoopCampaign.Payment_Status__c == 'Not Paid');
}
}
@isTest static void testSetPaymentCodes() {
User u = createUsersAndReturnOne();
System.runAs(u) {
Coop_Campaign__c coopCampaign = createCoopCampaign();
Payment_Request__c paymentRequest = [SELECT Id, Status__c FROM Payment_Request__c WHERE Coop_Campaign__c = :coopCampaign.Id];
PaymentRequestController.setPaymentCodes(paymentRequest.Id, '12', '2018');
paymentRequest = [SELECT Id, Payment_Code__c FROM Payment_Request__c WHERE Id = :paymentRequest.Id];
System.assert(paymentRequest.Payment_Code__c == 'COOP2018-123412');
}
}
private static Coop_Campaign__c createCoopCampaign() {
Account account = new Account(Dealer_Code__c = '1234', Name = 'Test Account');
insert account;
Coop_Budget__c coopBudget = new Coop_Budget__c();
coopBudget.Account__c = account.Id;
coopBudget.Amount__c = 1000;
coopBudget.Month__c = 1;
coopBudget.Year__c = '2018';
insert coopBudget;
Coop_Campaign__c coopCampaign = new Coop_Campaign__c();
coopCampaign.Account__c = account.Id;
coopCampaign.Media_Type__c = 'Radio';
coopCampaign.Campaign_Status__c = 'Draft';
coopCampaign.Campaign_Start_Date__c = Date.newInstance(2018, 1, 1);
coopCampaign.Campaign_End_Date__c = Date.newInstance(2018, 1, 1);
coopCampaign.Payment_Status__c = 'Pending DBM Approval';
insert coopCampaign;
Creative__c creative = new Creative__c();
creative.Name = 'Test Creative';
creative.Media_Type__c = 'Radio';
creative.Expiry_Date__c = Date.today().addDays(1);
insert creative;
ContentVersion newContentVersion = new ContentVersion();
newContentVersion.Title = 'Test';
newContentVersion.PathOnClient = 'Test.jpg';
newContentVersion.VersionData = Blob.valueOf('Test');
insert newContentVersion;
ContentVersion contentVersion = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :newContentVersion.Id];
ContentDocumentLink newContentDocumentLink = new ContentDocumentLink();
newContentDocumentLink.ContentDocumentId = contentVersion.ContentDocumentId;
newContentDocumentLink.LinkedEntityId = creative.Id;
newContentDocumentLink.ShareType = 'V';
insert newContentDocumentLink;
Creative_Request__c creativeRequest = new Creative_Request__c();
creativeRequest.Account__c = account.Id;
creativeRequest.Coop_Campaign__c = coopCampaign.Id;
creativeRequest.Creative__c = creative.Id;
creativeRequest.Customization_Required__c = true;
insert creativeRequest;
Invoice__c invoice = new Invoice__c();
invoice.Account__c = account.Id;
invoice.Name = 'Test Invoice';
invoice.Subtotal_Amount__c = 100;
invoice.Tax_Rate__c = 0.13;
invoice.Total_Amount__c = 113;
insert invoice;
Payment_Request__c paymentRequest = new Payment_Request__c();
paymentRequest.Account__c = account.Id;
paymentRequest.Amount_Claimed__c = 50;
paymentRequest.Coop_Campaign__c = coopCampaign.Id;
paymentRequest.Coop_Budget_Year__c = String.valueOf(Date.today().year());
paymentRequest.Claim_Start_Date__c = Date.newInstance(2018, 1, 1);
paymentRequest.Claim_End_Date__c = Date.newInstance(2018, 1, 1);
paymentRequest.Invoice__c = invoice.Id;
insert paymentRequest;
return coopCampaign;
}
private static User createUsersAndReturnOne() {
createUser('Brand User');
createUser('Finance User');
createUser('Dealer Business Manager');
return createUser('Acquisition User');
}
private static User createUser(String profileName) {
String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
Profile profile = [SELECT Id FROM Profile WHERE Name=:profileName];
UserRole userRole = [SELECT Id FROM UserRole WHERE Name=:profileName];
User user = new User(
Alias = 'standt',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'Testing',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
ProfileId = profile.Id,
TimeZoneSidKey = 'America/Los_Angeles',
UserName = uniqueUserName,
UserRoleId = userRole.Id
);
insert user;
return user;
}
}
Edited after test class modification
After modification, ran the test multiple times, below is the snip of the message I saw on the logs inside developer console -
Edited after the finding the correct picklist values
These are the values I see for the Media_Type__c picklist for both the objects -
I went ahead and modified that part of the test code to this way -
Coop_Campaign__c coopCampaign = new Coop_Campaign__c();
coopCampaign.Account__c = account.Id;
//coopCampaign.Media_Type__c = 'Radio';
coopCampaign.Media_Type__c = 'Print';
coopCampaign.Campaign_Status__c = 'Draft';
coopCampaign.Campaign_Start_Date__c = Date.newInstance(2019, 1, 1);
coopCampaign.Campaign_End_Date__c = Date.newInstance(2020, 1, 1);
coopCampaign.Payment_Status__c = 'Pending DBM Approval';
insert coopCampaign;
Coop_Campaign__c coopCampaign1 = new Coop_Campaign__c();
coopCampaign1.Account__c = account.Id;
//coopCampaign1.Media_Type__c = 'Radio';
coopCampaign.Media_Type__c = 'Print';
coopCampaign1.Campaign_Status__c = 'Draft';
coopCampaign1.Campaign_Start_Date__c = Date.newInstance(2018, 2, 2);
coopCampaign1.Campaign_End_Date__c = Date.newInstance(2018, 2, 2);
coopCampaign1.Payment_Status__c = 'Pending DBM Approval';
insert coopCampaign1;
Creative__c creative = new Creative__c();
creative.Name = 'Test Creative';
//creative.Media_Type__c = 'Radio';
creative.Media_Type__c = 'Print';
creative.Expiry_Date__c = Date.today().addDays(1);
insert creative;