1

I have a trigger on Account object which will create an opportunity if the industry type is 'Electronics' and it's working fine but i have a problem with test class so please help out.

Here is the trigger

trigger CreateNewOpportunity on Account (after insert, after update) {
    List<Opportunity> opportunities1 = new List<Opportunity>();
    for(Account acct:Trigger.new){
        if(acct.industry == 'Electronics'){
        Opportunity nopportunity = new Opportunity();
        nopportunity.Name = acct.Name;
        nopportunity.AccountId = acct.Id;
        nopportunity.Amount = 10000;
        nopportunity.StageName = 'Proposal';
        nopportunity.CloseDate = System.today() + 30;

        opportunities1.add(nopportunity);
        }
    }

    if(opportunities1.isEmpty()== false){
       Database.upsert(opportunities1);
    }
}

And here is my Test Class for the same i'm getting only 33% coverage

@isTest
private class opp_test_class {

    static testmethod void test_trigger(){
        account acct = new account(Name = 'sha');
        Opportunity opp = new Opportunity(Name = 'sha',StageName = 'Prospecting',closedate = System.today() + 30);

      //  acct.name = 'sha';
        insert acct;
        insert opp;
    }
}

7 Answers 7

7

You need to set the Account's industry field to 'Electronics', because you have a conditional in your trigger that tests for that. So, change your Account record instantiation in your test class to:

account acct = new account(Name = 'sha', Industry = 'Electronics');

You also have to set the AccountId field on the Opportunity prior to inserting the Opportunity, but after inserting the Account.

opp.AccountId = acct.Id
2

If you look in the Developer Console, you can see what lines are being hit. From your test code, you are creating an Account for your Opportunity that doesn't meet the IF condition in your trigger, so a bulk of your lines are being ignored.

http://wiki.developerforce.com/page/Developer_Console

Additionally, take a look at the best practices for unit testing. You'll want to use assert statements to ensure your code is actually being validated (instead of just covered).

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

1

Your Trigger code requires that the Account.Industry field is set to 'Electronics'.

If you add this field to the Account constructor in your test class you should get code coverage on the lines within the if(acct.industry == 'Electronics'){}

I would advise that you also include some asserts to check that the functionality you are trying to produce is working. Some useful examples of writing apex tests can be found on the developerforce wiki http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

1

Explanation: If you create account with industry type Electronics. then you are creating opportunity.[Note :Based on account Insertion we are adding opportunity right so we have to write after trigger]. So here you have to take care 2 steps in case of writing test class

step1: first create Account. Once you create the account you will get accountId.

step2: You attach that account Id to opportunity.

==========================================================================

//Handler class

trigger OppCreate on Account (after insert) {
  if(trigger.isInsert && trigger.isAfter){
    CreateNewOpportunity.method1(trigger.new);
  }
 }

===========================================================

//main class

public class CreateNewOpportunity {
   public static void method1(List<Account> aclist){
    List<Opportunity> oplist=new List<Opportunity>();
    for(Account acct:aclist){
        if(acct.industry == 'Electronics'){
            Opportunity op = new Opportunity();
            op.Name = acct.Name;
            op.AccountId = acct.Id;
            op.Amount = 100;  
            op.StageName = 'Proposal';
            op.CloseDate = System.today() + 30;
            oplist.add(op);
        }
    }

    if(!oplist.isEmpty()){
        insert oplist;
    }
}
}

====================================================

test class

  @isTest
public class CreateNewOpportunity_Test {
public static testmethod void oppTest(){
    Account acct=new Account();
        acct.name='a';
        acct.industry='Electronics';   
        insert acct;
        Opportunity op = new Opportunity();
          op.Name = acct.Name;
          op.AccountId = acct.Id;
          op.Amount = 100;
          op.StageName = 'Proposal';
          op.CloseDate = System.today() + 30;
          insert op;
      }
 }

=========================================================================
Make it as best answer it resolves your query.It helpful for other people for searching for similar answers

0

try this:

@isTest
private class opp_test_class {

   static testmethod void test_trigger(){
        account acct = new account(Name = 'sha', Industry == 'Electronics');
        insert acct;

        Opportunity opp = [SELECT Account FROM Opportunity WHERE Name = :acct.Name LIMIT 1];
        System.assertEquals(acct.Id, opp.Account);
    }
}
0
@isTest
public class CreateNewOpportunityTest {

        static testmethod void test_trigger(){
        account acct = new account(Name = 'sha', Industry = 'Electronics');
        insert acct;

        Opportunity opp = new Opportunity(AccountId = acct.Id,Name = 'sha',StageName = 'Prospecting',closedate = System.today() + 30);

        insert opp;

}

}
1
  • Can you explain a little your answer ?
    – SF_user
    Dec 17, 2014 at 10:06
-1

I would like to add that once you create Account test data factory with Account Name and Account Industry as Electronics then that's it. 100% coverage for the class. You do not need to create any sample test data for Opportunity.

Test Class

Coverage

9
  • Please use comments if you want to add something to an existing answer. Or answer fully to the question. Feb 22, 2017 at 10:25
  • i dont have enough reputation to add comments Feb 22, 2017 at 10:26
  • So try to gain reputation. Feb 22, 2017 at 10:27
  • yeah :D :P :D :P Feb 22, 2017 at 10:28
  • 1
    To which answer did you want to comment ? Feb 22, 2017 at 12:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .