1

I have a simple trigger but enable to cover the code coverage please anyone idea about this?

trigger ClosedOpportunityTrigger on Opportunity (before insert) 
{   
    list<task> carry = new list<task>();
    for(opportunity opp: trigger.new)
    {
        if(opp.StageName == 'Closed Won')
        {
           task t = new task(whatid=opp.id,Subject = 'Follow Up Test Task');
            carry.add(t); 
        }
   }
     insert carry;
}

Here is My test class

@isTest
public class TestOppTrig 
{
    static testmethod void testFun()
    {
        Opportunity opp = new opportunity();
        opp.Name = 'Bob';
        opp.StageName = 'closed won';
        opp.CloseDate = system.today();
        insert opp


        task t = new task();
        t.whatid=opp.id;
        t.subject = 'dasfsdfds';
        insert t;
    }
}

Here is the Screen Shot

enter image description here

  • Here is the screen shot – Bonny Jan 19 '16 at 6:09
  • Please Take a Look. – Bonny Jan 19 '16 at 6:19
  • Hi Ratan Thanks for help.I will sort out this problem with my end – Bonny Jan 19 '16 at 6:46
  • Bonny Sure.. Let me know once it works or any other issue you face. Thanks – Ratan Paul Jan 19 '16 at 6:47
  • Hi Ratan. It was my mistake Code was right but i inactive that trigger that's why its passed the method but did not show coverage. – Bonny Jan 19 '16 at 7:26
2

Trigger should be on after insert.. Because in before context we don't get opp Id

Since you trigger on opportunity before insert you need to insert opportunity

insert opp; this will fire your trigger

JUst insert opportunity. then your trigger will create a Task record.

So put System.assertEquals to check if there is any task created on opportunity or not

@isTest
public class TestOppTrig 
{
    static testmethod void testFun()
    {
        Opportunity opp = new opportunity();
        opp.Name = 'Bob';
        opp.StageName = 'closed won';
        opp.CloseDate = system.today();
        insert opp;
        System.assertEquals([SELECT Id FROM Task WHERE whatid =: opp.Id].size(), 1);
    }
}

In you developer console. You need to check Run as Asynchronous checkbox and then run your test class

enter image description here

  • I add "insert" statement but it also show 0 percentage coverage. – Bonny Jan 19 '16 at 5:27
  • @Bonny are you running test class in developer console. If yes you need to check run as Asynchronous checkbox – Ratan Paul Jan 19 '16 at 5:29
  • Hello Ratan its Shows the test method is pass but not shown the code coverage. – Bonny Jan 19 '16 at 5:39
  • @Bonny check my updated Ans.. If that won't work then I suggest run your test class from any IDE – Ratan Paul Jan 19 '16 at 5:41
  • Hi Ratan thanks for assisting i did the same as you told but i did not show the any code coverage – Bonny Jan 19 '16 at 5:48
0

You have your trigger on opportunity so insert opportunity in your test class.

@isTest
public class TestOppTrig 
{
    static testmethod void testFun()
    {
        Opportunity opp = new opportunity();
        opp.Name = 'Bob';
        opp.StageName = 'closed won';
        opp.CloseDate = system.today();

       insert opp;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.