+ Start a Discussion
KyoKyo 

I have a problem Testclass on the Trigger

CodeCoverage 50%. I want more 75 % 

 

 

trigger PopAccount on Case (before insert,before update) {
    for(Case ca : trigger.new){   
    if(ca.AccountID == null && ca.Account_Number__c != null){
        Account acc = [select id,AccountNumber From Account Where AccountNumber  =: ca.Account_Number__c];
        ca.AccountID = acc.id;  
        }
    }
}

 

@isTest 
private class TestPopAccount{
    static testMethod void myTest() {
    
        
          
        Account Acc = new Account();
        Acc.Name = 'test'; 
        insert Acc;
        
        Acc = [select id,AccountNumber,Type From Account Where id =: acc.id];
        
        
        Contact con = new Contact();
        Con.AccountID = Acc.id;
        Con.LastName = 'Test';
        insert con;
        
        Case ca = new Case();
        ca.Account_Number__c  = '000001';
        ca.AccountID = Acc.id;
        ca.ContactID = con.id;
        ca.Origin = 'Phone';
        ca.Status = 'Closed';
        insert ca;
        update ca;

 Thank you so much.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

In your method you are using this

 

trigger PopAccount on Case (before insert,before update) {
    for(Case ca : trigger.new){   
    if(ca.AccountID == null && ca.Account_Number__c != null){
        Account acc = [select id,AccountNumber From Account Where AccountNumber  =: ca.Account_Number__c];
        ca.AccountID = acc.id;  
        }
    }
}

You are checking ca.AccountID == null but it is not as you have set this in test method

 

@isTest 
private class TestPopAccount{
    static testMethod void myTest() {
    
        
          
        Account Acc = new Account();
        Acc.Name = 'test'; 
        insert Acc;
        
        Acc = [select id,AccountNumber,Type From Account Where id =: acc.id];
        
        
        Contact con = new Contact();
        Con.AccountID = Acc.id;
        Con.LastName = 'Test';
        insert con;
        
        Case ca = new Case();
        ca.Account_Number__c  = '000001';
        ca.AccountID = Acc.id;
        ca.ContactID = con.id;
        ca.Origin = 'Phone';
        ca.Status = 'Closed';
        insert ca;
        update ca;

 

 

So you will never enter the condition, After fixing this you code coverage will increase but test might fail as you have not set AccountNumber for inserted account in test method so 

 

 

Account acc = [select id,AccountNumber From Account Where AccountNumber  =: ca.Account_Number__c]

 

This will not return any record, plesse fix this also by setting the accountnumber in test method when you insert account.

 

 

All Answers

Shashikant SharmaShashikant Sharma

In your method you are using this

 

trigger PopAccount on Case (before insert,before update) {
    for(Case ca : trigger.new){   
    if(ca.AccountID == null && ca.Account_Number__c != null){
        Account acc = [select id,AccountNumber From Account Where AccountNumber  =: ca.Account_Number__c];
        ca.AccountID = acc.id;  
        }
    }
}

You are checking ca.AccountID == null but it is not as you have set this in test method

 

@isTest 
private class TestPopAccount{
    static testMethod void myTest() {
    
        
          
        Account Acc = new Account();
        Acc.Name = 'test'; 
        insert Acc;
        
        Acc = [select id,AccountNumber,Type From Account Where id =: acc.id];
        
        
        Contact con = new Contact();
        Con.AccountID = Acc.id;
        Con.LastName = 'Test';
        insert con;
        
        Case ca = new Case();
        ca.Account_Number__c  = '000001';
        ca.AccountID = Acc.id;
        ca.ContactID = con.id;
        ca.Origin = 'Phone';
        ca.Status = 'Closed';
        insert ca;
        update ca;

 

 

So you will never enter the condition, After fixing this you code coverage will increase but test might fail as you have not set AccountNumber for inserted account in test method so 

 

 

Account acc = [select id,AccountNumber From Account Where AccountNumber  =: ca.Account_Number__c]

 

This will not return any record, plesse fix this also by setting the accountnumber in test method when you insert account.

 

 

This was selected as the best answer
KyoKyo

I can edit it, then Code Coverage 75%, but Error 

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopAccount: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.PopAccount: line 4, column 23: []

 


Shashikant SharmaShashikant Sharma

Change this

 

Account Acc = new Account();
        Acc.Name = 'test'; 
        insert Acc;

 

 

to

 

 

Account Acc = new Account();
Acc.Name = 'test'; 
acc.AccountNumber = '000001';
insert Acc;

 

 

 

KyoKyo

Thank! Perfect!

Shashikant SharmaShashikant Sharma

Your welcome , If you want to know structure of test method please check this

 

http://forceschool.blogspot.com/2011/06/testing-apex-structure-of-test-class.html