List<Opportunity> listOpp = new List<Opportunity>(); oppNew = [SELECT Id,StageName FROM Opportunity WHERE Id =:fr.Opportunity__c]; if(oppNew.StageName == 'To Be Searched') { oppNew.StageName = 'Search'; listOpp.add(oppNew); }
List<FF__c> ffList = new List<FF__c>(); ffList.add(ff);
List<Opportunity> listOpp = new List<Opportunity>(); for(FF__c f : ffList) { oppNew = [SELECT Id,StageName FROM Opportunity WHERE Id =:f.Opportunity__c]; try { boolean flag = true; if (ff.Account_Type__c == 'ANA' && ff.DOB__c == null) { ff.DOB__c.addError('DOB is required'); flag = false; } if (ff.Account_Type__c == 'DL' && ff.Last_Name__c == null) { ff.Last_Name__c.addError('Last Name is required'); flag = false; }
run the each test class seperately in User Interface and i think u r running in eclipse so it will show like that no problem ur both trigger having 100% u can deploy to PD.
Hi
try this it will getting 100%.
All Answers
Hi
try this it will getting 100%.
Try making your test class private. I also updated the test method. Let me know if this works for you.
@isTest
private class FlightRequestTestCase {
static testmethod void myUnitTest() {
Opportunity oppNew = new Opportunity( Name = 'Test Opp'
StageName = 'To Be Searched'
CloseDate = System.today() );
insert oppNew;
Flight_Request__c fr = new Flight_Request__c( From__c = 'Ani'
To__c = 'Ani'
Opportunity__c = oppNew.Id );
insert fr;
// Assert that the trigger actually worked.
Opportunity opp = [ select StageName from Opportunity where Id = :oppNew.Id limit 1 ];
System.assertEquals( 'Search', opp.StageName );
}
}
Hope that helps!
~ Clint
@SRS8
System.assertEquals() is not required to check output of test case?
Hi
if u getting 100% code here then no need to have we can directly insert those values.
ofcourse it is one of the best practice to use in test classes.
Let me know any issue and if it solves ur problem plz accept it solution.
@SRS8, Yes its works, but giving error for following trigger
Please suggest Test case for following trigger also
trigger FFTrigger on FF__c (after insert) {
List<Opportunity> listOpp = new List<Opportunity>();
boolean flag = true;
for (FF__c ff: trigger.new)
{
if (ff.Account_Type__c == 'ANA' && ff.DOB__c == null)
{
ff.DOB__c.addError('DOB is required');
flag = false;
}
if (ff.Account_Type__c == 'DL' && ff.Last_Name__c == null)
{
ff.Last_Name__c.addError('Last Name is required');
flag = false;
}
if(flag)
{
//Opportunity oppNew = new Opportunity(Id = ff.Opportunity__c);
Opportunity oppNew = [SELECT Id,StageName FROM Opportunity WHERE Id =:ff.Opportunity__c];
if(oppNew.StageName == 'Ticketing')
{
oppNew.StageName = 'FF Account Assigned';
listOpp.add(oppNew);
}
}
}
if (listOpp != null && !listOpp .isEmpty())
{
Database.update(listOpp);
}
}
Test case is
@isTest
private class FFTestCase {
private static testMethod void myUnitTest() {
Opportunity oppNew = new Opportunity();
oppNew.Name = 'Test Opp';
oppNew.StageName = 'Ticketing';
oppNew.CloseDate = System.now().date();
insert oppNew;
FF__c ff = new FF__c();
ff.Account_Number__c ='1234456';
ff.Account_Type__c = 'ANA';
ff.DOB__c = System.now().date();
ff.Opportunity__c = oppNew.Id;
ff.Last_Name__c = 'Dutt';
insert ff;
List<FF__c> ffList = new List<FF__c>();
ffList.add(ff);
List<Opportunity> listOpp = new List<Opportunity>();
for(FF__c f : ffList)
{
oppNew = [SELECT Id,StageName FROM Opportunity WHERE Id =:f.Opportunity__c];
try
{
boolean flag = true;
if (ff.Account_Type__c == 'ANA' && ff.DOB__c == null)
{
ff.DOB__c.addError('DOB is required');
flag = false;
}
if (ff.Account_Type__c == 'DL' && ff.Last_Name__c == null)
{
ff.Last_Name__c.addError('Last Name is required');
flag = false;
}
if(flag)
{
if(oppNew.StageName == 'Ticketing')
{
oppNew.StageName = 'FF Account Assigned';
listOpp.add(oppNew);
}
}
if (listOpp != null && !listOpp .isEmpty())
{
Database.update(listOpp);
}
System.assertEquals('FF Account Assigned',oppNew.StageName);
}
catch (System.DmlException e){
System.debug('we caught a dml exception: ' + e.getDmlMessage(0));
}
}
}
}
Hi
Try this code
@isTest
private class FFTestCase {
private static testMethod void myUnitTest() {
Opportunity oppNew = new Opportunity();
oppNew.Name = 'Test Opp';
oppNew.StageName = 'Ticketing';
oppNew.CloseDate = System.now().date();
insert oppNew;
FF__c ff = new FF__c();
ff.Account_Number__c ='1234456';
ff.Account_Type__c = 'ANA';
ff.DOB__c = null;
ff.Opportunity__c = oppNew.Id;
ff.Last_Name__c = 'Dutt';
insert ff;
FF__c ff1 = new FF__c();
ff1.Account_Number__c ='1234456';
ff1.Account_Type__c = 'DL';
ff1.DOB__c = System.now().date();
ff1.Opportunity__c = oppNew.Id;
ff1.Last_Name__c =null;
insert ff1;
Boolean flag= true;
}}
Regards,
S Rajesh.
@SRS8, test case fail
error is "Custom validation exception DOB is required"
and Code Covrage is 56%
Hi
what i sent to u plz replace system.today() on DOB and can u post the line in trigger which r not covered.
Regards,
S Rajesh.
@SRS8
Thanks aloft for your help
I set DOB = system.today() and LastName = 'D'
then test run successfully
But
When i run FlighRequestTestCase it says
FFTrigger -- 16 lines not tested, 0% covered
FlightRequestTrigger -- 100% covered
FlighRequestTestCase -- 100% covered
AND
When i run FFTestCase it says
FlightRequestTrigger -- 9 lines not tested, 0% covered
FFTrigger -- 9 lines not tested, 75% covered
FFTestCase -- 100% covered
Any idea?
try
@isTest (seeAlldata=true)
@j020
sorry , it didn't work
Hi
there are 2 triggers right? and both having code coverage 100%?
can i have the screen shot plz.
@SRS8
yes , there are 2 triggers with two test cases
When i run FlighRequestTestCase it says
FFTrigger -- 16 lines not tested, 0% covered
FlightRequestTrigger -- 100% covered
FlighRequestTestCase -- 100% covered
AND
When i run FFTestCase it says
FlightRequestTrigger -- 9 lines not tested, 0% covered
FFTrigger -- 9 lines not tested, 75% covered
FFTestCase -- 100% covered
Not sure how to actach image
Hi
Send ur gmail id and i will add in google+.
anil@swiftsetup.com
Hi
run the each test class seperately in User Interface and i think u r running in eclipse so it will show like that no problem ur both trigger having 100% u can deploy to PD.
@SRS8
yes you ar right, i m using eclippse , triggers and test deployed to Force.com with 100% and 75% coverage
So it is eclipsse behaviour which was creating problem
Thanks alot for you help
Hi
Good to hear :)
Make it as solution so that some one will get use of it.