I'm trying to cover my Apex Trigger below using a Test Class:
trigger createPubAppovalCase on Opportunity (after update) {
List<Case> listcase = new List<Case>();
for(Opportunity o:trigger.new) {
Opportunity oldopp = trigger.OldMap.get (o.id);
if (o.Request_PubApproval__c && !oldopp.Request_PubApproval__c && o.Account.Account_Region__c <> 'North America' && o.Account.Account_Region__c <> '' && o.Account.Account_Region__c <> null)
{
String userName = UserInfo.getUserName();
User activeUser = [Select Email From User where Username = : userName limit 1];
String userEmail = activeUser.Email;
List<Contact> oppOwnerAsContact = [select Id from Contact where email =: userEmail];
//If the above list is empty, a new Contact needs to be created before the Case is inserted.
if(oppOwnerAsContact.size() == 0) {
Contact newContact = new Contact(firstName = UserInfo.getFirstName(),
lastName = UserInfo.getLastName(),
Account = [select id from Account where Name =: 'LiveRail' Limit 1],
email = userEmail);
insert newContact;
oppOwnerAsContact.add(newContact);
}
//here Opportunity__c is lookup to Opportunity record for which new record is created
listCase.add(new Case(Opportunity__c = o.id,
RecordTypeID = [select id from RecordType where DeveloperName = :'Pub_Approval' Limit 1].id,
OwnerID = [select id from Group where DeveloperName = :'EMEA_Pub_Approval_Queue' Limit 1].id,
ContactID = oppOwnerAsContact[0].id,
Subject = 'Pub Approval for "'+o.Name+'"',
Origin = 'Pub Approval Workflow',
//Priority = 'Medium',
Status = 'New',
Description = 'Full Details on the opportunity.',
site_URLs__c = o.Site_URLs__c,
Sample_video_page_URL__c = o.Sample_video_page_URL__c,
Company_website__c = o.Company_website__c));
}
}
if(listcase.size()>0) {
insert listcase;
}
Below is the test Class, which for some reason isn't covering the above trigger, and the case isn't being created (no rows for assignment error). I'm stumped!
@isTest (SeeAllData=true)
private class testPubApprovalTrigger {
static testMethod void testPubApprovalRequest() {
Account acc = new Account(Name = 'testestsklzjgfldsa',
Account_Region__c = 'EMEA',
Account_Country__c = 'United Kingdom',
Type = 'Publisher');
insert acc;
acc = [select id, Name from Account where name = 'testestsklzjgfldsa' Limit 1];
User own = [select id, Name from User where name = 'Tim Milazzo' Limit 1];
Opportunity opp = new Opportunity(name='pubpubpubApprovalTest',
closeDate=date.today(),
recordType = [select id from RecordType where DeveloperName = 'LRC_Supply' AND SobjectType = 'Opportunity'],
site_URLs__c = 'site.com',
sample_Video_Page_URL__c = 'site.com/video',
company_website__c = 'site.com',
stageName = 'Pitching',
Account = acc,
Owner = own,
LeadSource = 'apex is so fun');
insert opp;
opp = [select id, Request_PubApproval__c, Name, ownerID from Opportunity where name =: 'pubpubpubApprovalTest' Limit 1];
opp.Request_PubApproval__c = TRUE;
update opp;
// Find the new case and test some assertions
Case appCase = [select id, sample_Video_Page_URL__c from Case where Opportunity__c =: opp.id Limit 1];
system.assert(appCase.sample_Video_Page_URL__c == opp.sample_Video_Page_URL__c);
}
}
If this is easy for anyone to catch what's going wrong, it would be very much appreciated!
{}
for preformatted text) or space each line over 4 spaces. Have you tried adding debug statements to see where your code isn't working?