+ Start a Discussion
Shantanu Punekar 5Shantanu Punekar 5 

Test class on Trigger to create Task on Opportunity update.

Hello all,

I have created below trigger and a test class for it, still its not covering the trigger. What needs to be changed in this test class?
Please help

trigger OppTask on Opportunity (after update) {
    List <Contact> conList = [SELECT ID,AccountID FROM Contact WHERE AccountId IN (SELECT AccountId FROM Opportunity WHERE Id IN :Trigger.new)];
    map<ID,Contact> accountIDWithContactMap = new map<ID,Contact>();
    for(Contact con : conList){
        accountIDWithContactMap.put(con.AccountID, con);
    for(Opportunity Opp : Trigger.new){
        if(Trigger.newMap.get(opp.Id).StageName != Trigger.oldMap.get(opp.Id).StageName && opp.StageName == 'Closed Won' && accountIDWithContactMap.containsKey(Opp.AccountId)){
            Task T = new Task();
            T.WhatId = opp.Id;            
            T.OwnerId = opp.OwnerId;
            T.Subject = 'Call';
            T.Status = 'In Progress';
            T.IsReminderSet = TRUE;
            T.WhoId = accountIDWithContactMap.get(Opp.AccountId).ID;
            T.ReminderDateTime = DateTime.now().addHours(24);
            insert T;

Test Class:

public class testOppTaskTrigger{
    static testMethod void ValidateOppTaskTrigger(){
    Account testAcc = new Account (Name = 'Test Account');
    insert testAcc;
    User U = [SELECT ID, Name FROM User WHERE Profile.Name = 'System Administrator'];
    Opportunity testOpp = new Opportunity (Name = 'Test Name',
                                     OwnerId =U.Id,
                                     AccountId = testAcc.Id,
                                     StageName = 'Open',
                                     Amount = 50000.00,
                                     CloseDate = System.today()
    insert testOpp;
    Opportunity testOpp2 = [SELECT Name, Id, StageName FROM Opportunity WHERE Id =: testOpp.Id];
                            if(testOpp2.StageName == 'Closed Won') {
                            update testOpp;
    Task testTask = new Task ( WhatId = testOpp2.Id,            
            OwnerId = testOpp2.OwnerId,
            Subject = 'Call',
            Status = 'In Progress',
            IsReminderSet = TRUE,
            ReminderDateTime = DateTime.now().addHours(24));                                
     insert testTask;

Shrikant BagalShrikant Bagal
@Shantanu, you need to create one contact belong to "Test Name" account in test Methods, because In your trigger code you are applying  "accountIDWithContactMap.containsKey(Opp.AccountId)" condition which will false through text method.
HI Shantanu,

Try with below code it will give you 100% code coverage ,
public class testOppTaskTrigger{
    static testMethod void ValidateOppTaskTrigger(){
        Account testAcc = new Account ();
            testAcc.Name = 'Test Account';
            insert testAcc;
			testAcc=[SELECT id,Name FROM Opportunity WHERE Id =:testAcc.Id];
		  System.assertEquals(testAcc.Name,'Test Account');
        Contact con=new Contact();
           Insert con;
        con=[SELECT id,Name FROM Opportunity WHERE Id =:con.Id];
        Opportunity testOpp = new Opportunity ();
            testOpp.Name = 'Test Opp';
            testOpp.AccountId = testAcc.Id;
            testOpp.StageName = 'Open';
            testOpp.Amount = 50000.00;
            testOpp.CloseDate = System.today();
          insert testOpp;
		  testOpp=[SELECT id,Name FROM Opportunity WHERE Id =:testOpp.Id];
		  System.assertEquals(testOpp.Name,'Test Opp');
           testOpp.StageName = 'Closed Won';
				update testOpp;

Let me know if it helps .


Shantanu Punekar 5Shantanu Punekar 5
Thank you Manoj, Shrikant. I'll try this.
Shantanu Punekar 5Shantanu Punekar 5
Hi Manoj,
Why would we need the soql on lines 7, 14 and 23 which is selecting Id from opp? Obviously it will give an error while compiling. And why do we need to create a Contact record for this?
Hi Shantanu,

Have tried with the code ? It is best practice to check with Either System.assert/assertEquals/assertNotEquals to check the record is inserted or not .

I think will not give you compile time or run time error .

To know more about the test class please check below link it has a lot of links which will give you a lot of infromation about test class .

Let me if it help or still you have doubt .

Shrikant BagalShrikant Bagal
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.